Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I need help to query exchange server (get mail box details etc...) using a (any) programming language. I need to create an application which gives the values for different exchange server parameters (like mailbox size). I have already created a C# (with power-shell) sample for this and that is not working from a remote machine. Any help on this would be greatly appreciated.

Regards
Sebastian
Posted
Comments
Sandeep Mewara 19-Sep-12 2:23am    
that is not working from a remote machine
You get any error?
Sebastian T Xavier 19-Sep-12 2:53am    
Yes, Here is the code which I have tried.....

--------------------------------------------------------------
try
{
string userName = @"abc\Administrator";
string password = "xxx";
SecureString secureString = new SecureString();
foreach (char c in password) secureString.AppendChar(c);
secureString.MakeReadOnly();
PSCredential credential = new PSCredential(userName, secureString);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://192.168.1.221/powershell/"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential) {AuthenticationMechanism = AuthenticationMechanism.Basic };
Runspace runspace = RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", new Uri("http://192.168.1.221/powershell/"));
command.AddParameter("Credential", credential);
command.AddParameter("Authentication", "Basic");
powershell.Commands = command;
runspace.Open();
powershell.Runspace = runspace;
Collection<PSSession> result = powershell.Invoke<PSSession>();
foreach (ErrorRecord current in powershell.Streams.Error)
MessageBox.Show(String.Format("The following Error happen when opening the remote Runspace: {0} | InnerException: {1}",current.Exception, current.Exception.InnerException));
if (result.Count != 1)
throw new Exception("Unexpected number of Remote Runspace connections returned.");
powershell = PowerShell.Create();
command = new PSCommand();
command.AddCommand("Set-Variable");
command.AddParameter("Name", "ra");
command.AddParameter("Value", result[0]);
powershell.Commands = command;
powershell.Runspace = runspace;
powershell.Invoke();

powershell = PowerShell.Create();
command = new PSCommand();
const string CreateMailBoxScript = "new-mailbox -Name:’{0}’ -Password:(ConvertTo-SecureString {1} -AsPlainText -Force) -WindowsLiveID:{2} -FirstName:{3} -LastName:{4} -DisplayName:’{5}’";
command.AddScript(string.Format(CreateMailBoxScript, "sxavier", "textboxwithmail", "s@hotmail.com", "Sebastian", "Xavier", "Sebastian Xavier"));
powershell.Commands = command;
powershell.Runspace = runspace;
powershell.Invoke();

runspace.Dispose();
runspace = null;
powershell.Dispose();
powershell = null;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
-----------------------------------------------------------------------
And the exception is ....


System.Management.Automation.Remoting.PSRemotingTransportException:
Connecting to remote server failed with the following error message : The WinRM client cannot process the request. The authentication mechanism requested by the client is not supported by the server or unencrypted traffic is disabled in the service configuration. Verify the unencrypted traffic setting in the service configuration or specify one of the authentication mechanisms supported by the server. To use Kerberos, specify the computer name as the remote destination. Also verify that the client computer and the destination computer are joined to a domain. To use Basic, specify the computer name as the remote destination, specify Basic authenti
Sandeep Mewara 19-Sep-12 3:20am    
See if this helps:
http://support.microsoft.com/kb/2028305

Basically, look at the link from configuring environment prospective and check.
Sebastian T Xavier 19-Sep-12 3:25am    
Thanks; I had already gone through this(That's why I was looking for some different solutions other than c#). Will confirm once again.
Sergey Alexandrovich Kryukov 19-Sep-12 20:44pm    
How on Earth switching to any other solution can help if you cannot get it right with C#? It will get you nowhere.
--SA

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900