none
Excute ESM of Exchange in C# code ,encounter the error "The WinRM client cannot process the request." RRS feed

  • Question

  • I  try to excute ESM of Exchage in my C# code.

    My code as follow:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    using System.Management.Automation.Remoting;
    using System.Collections.ObjectModel;
    using System.Security;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                
                //RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
                //PSSnapInException snapInException = null;
                //PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
                //Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
                //myRunSpace.Open(rsConfig);
                Runspace myRunspace = RunspaceFactory.CreateRunspace();
                myRunspace.Open();
                Pipeline pipeline = myRunspace.CreatePipeline();
                Command myCommand = new Command("get-command");
                //CommandParameter verbParam = new CommandParameter("Verb", "Get");
                //myCommand.Parameters.Add(verbParam);
                pipeline.Commands.Add(myCommand);
                Command myMember = new Command("get-member");
                pipeline.Commands.Add(myMember);
                Collection<PSObject> CommandResults = pipeline.Invoke();
                string liveIDConnectionUri = "http://129.42.9.216/powershell?serializationLevel=Full";
                string ExchangePwd = "Huawei123";
                SecureString pwd = new SecureString();
                foreach(char c in ExchangePwd.ToCharArray())
                {
                    pwd.AppendChar(c);
                }
                
                PSCredential ExchangeCredential = new PSCredential("seg\administrator",pwd);
                System.Uri serverUri = new Uri(liveIDConnectionUri);
                WSManConnectionInfo connectionInfo = new WSManConnectionInfo( serverUri, "http://schemas.microsoft.com/powershell/Microsoft.Exchange", ExchangeCredential);
                using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo))
                {
                    PowerShell psh = PowerShell.Create();
                    psh.Runspace = rs;
                    rs.Open();
                    psh.AddArgument("get-mailboxdatabase");
                    Collection<PSObject> results = psh.Invoke();
                    foreach (PSObject cmdlet in results)
                    {
                        string cmdletName = cmdlet.Properties["Name"].Value.ToString();
                        Console.WriteLine(cmdletName);
                    }
                
                }
            }
        }
    }

    But when it run , encountering a error .

    I debuged My code, The error is come from this line "rs.Open();"

    The error information is :

    "Connecting to remote server failed with the following error message : The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.“

    How can I deal with this error?

    Saturday, June 2, 2012 7:00 AM

All replies

  • You should be using https (which is the default on Exchange anyway) eg

    string liveIDConnectionUri = "https://

    If you have a standard Exchange 2010 install then something like this should work (even if you dont have a regiistered cert)

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://" + snServerName + "/PowerShell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential); connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; connectionInfo.SkipCACheck = true; connectionInfo.SkipCNCheck = true;
                    connectionInfo.MaximumConnectionRedirectionCount = 4;
                    Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

    Cheers
    Glen
    Monday, June 4, 2012 7:03 AM
  • I am using the https,

    string liveIDConnectionUri = "https://129.42.9.216/powershell?serializationLevel=Full";

    But I got another error :

    System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server failed with the following error message : The ser
    ver certificate on the destination computer (129.42.9.216:443) has the following errors:
    The SSL certificate is signed by an unknown certificate authority.
    The SSL certificate contains a common name (CN) that does not match the hostname. For more information, see the about_Remote_Troubleshooting Help topic.
       at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
       at System.Management.Automation.Runspaces.Internal.RunspacePoolInternal.EndOpen(IAsyncResult asyncResult)
       at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal.Open()
       at System.Management.Automation.Runspaces.RunspacePool.Open()
       at System.Management.Automation.RemoteRunspace.Open()

    Wednesday, June 6, 2012 3:18 AM
  • Did you try the code sample I posted ? more importantly the section

                    connectionInfo.SkipCACheck = true;
                    connectionInfo.SkipCNCheck = true;

    demonstrates how to deal with the certificate error your getting

    Cheers
    Glen

    Wednesday, June 6, 2012 3:54 AM
  • I have tried your code.

    string liveIDConnectionUri = "https://129.42.9.216/powershell?serializationLevel=Full";
                string ExchangePwd = "Huawei123";
                SecureString pwd = new SecureString();
                foreach(char c in ExchangePwd.ToCharArray())
                {
                    pwd.AppendChar(c);
                }
                
                PSCredential ExchangeCredential = new PSCredential("seg\administrator",pwd);
                System.Uri serverUri = new Uri(liveIDConnectionUri);
                WSManConnectionInfo connectionInfo = new WSManConnectionInfo( serverUri, "http://schemas.microsoft.com/powershell/Microsoft.Exchange", ExchangeCredential);
                connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
                connectionInfo.SkipCACheck = true;
                connectionInfo.SkipCNCheck = true;
                connectionInfo.MaximumConnectionRedirectionCount = 4;

    And  got the error :

    Connecting to remote server failed with the following error message : The WinRM client cannot process the request. Unencrypted traffic is currently disabled in the client configuration. Change the client configuration and try the request again. For more information, see the about_Remote_Troubleshooting Help topic.

    Wednesday, June 6, 2012 6:49 AM
  • I would go back to just using Powershell first to test if you can successfully make a remote connection to the Exchange server if you can't make this work for a powershell session then it won't work in your code. That error points to a client side policy issue with WSMan

    You may also need to look at adding this a trusted host http://technet.microsoft.com/en-us/magazine/ff700227.aspx (also use the FQDN from the SSL cert instead of the IPAddress)

    Cheers
    Glen

    Wednesday, June 6, 2012 7:25 AM