Click here to Skip to main content
14,735,733 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello All,

I was trying to run exchange server 2010 monitoring commands using C#. Here is my code...

string password = "abc123"; 
string userName = @"me.com\Administrator";
System.Uri uri = new Uri("http://192.168.1.194/powershell?serializationLevel=Full");
System.Security.SecureString securePassword = String2SecureString(password);
System.Management.Automation.PSCredential creds = new System.Management.Automation.PSCredential(userName, securePassword);
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", uri);
command.AddParameter("Credential", creds);
command.AddParameter("Authentication", "Default");
PSSessionOption sessionOption = new PSSessionOption();
sessionOption.SkipCACheck = true;
sessionOption.SkipCNCheck = true;
sessionOption.SkipRevocationCheck = true;
command.AddParameter("SessionOption", sessionOption);
powershell.Commands = command;
try
   {
       runspace.Open();
       powershell.Runspace = runspace;
       Collection<PSSession> result = powershell.Invoke<PSSession>();
       foreach (ErrorRecord current in powershell.Streams.Error)
       {
             throw new Exception("Exception: " + current.Exception.ToString());
       }
       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();
       command.AddScript("Import-PSSession -Session $ra");
       powershell.Commands = command;
       powershell.Runspace = runspace;
       powershell.Invoke();
 
       System.Collections.ObjectModel.Collection<PSObject> results = new 
       System.Collections.ObjectModel.Collection<PSObject>();
 
       powershell = PowerShell.Create();
       powershell.Runspace = runspace;
 
       System.IO.StreamReader sr = new System.IO.StreamReader("..\\..\\Script.ps1");
       powershell.AddScript(sr.ReadToEnd());
       powershell.Runspace.SessionStateProxy.SetVariable("proc", "C*");
       powershell.Runspace.SessionStateProxy.SetVariable("mbx", "*MBX");
 
       results = powershell.Invoke();
 
       if (powershell.Streams.Error.Count > 1)
       {
           foreach (ErrorRecord er in powershell.Streams.Error)
                  Console.WriteLine(er.ErrorDetails);
       }
       else
       {
           foreach (PSObject ps in results)
           {
                   Console.WriteLine(ps.Properties["Name"].Value.ToString());
           }
       }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        runspace.Dispose();
        runspace = null;
        powershell.Dispose();
        powershell = null;
     }


But I have got the following error...

Exception: System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server failed with the following error message : The WinRM client cannot process the request. The WinRM client tried to use Negotiate authentication mechanism, but the destination computer (192.168.1.194:80) returned an 'access denied' error. Change the configuration to allow Negotiate authentication mechanism to be used or specify one of the authentication mechanisms supported by the server. To use Kerberos, specify the local 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 local computer name as the remote destination, specify Basic authentication and provide user name and password. Possible authentication mechanisms reported by server: For more information, see the about_Remote_Troubleshooting Help topic.


Any help to solve this would be great...

Thanks & Regards

Sebastian
Posted
Updated 15-Jun-12 7:08am
v5

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