PowerShell远程命令因未加密的流量而失败

时间:2013-02-25 00:03:13

标签: c# powershell exchange-server powershell-v2.0

运行以下代码:

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

        Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "first.last");
        command.AddParameter("Alias", "Fist Last");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            var result = powershell.Invoke();
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }

        Console.WriteLine("done");
        Console.Read();

异常抛出:

  

连接到远程服务器失败,并显示以下错误消息:   WinRM客户端无法处理请求。未加密的流量是   目前在客户端配置中已禁用。更改客户端   配置并再次尝试请求。有关更多信息,请参阅   about_Remote_Troubleshooting帮助主题。

我已经设置了基本身份验证,允许未加密的流量。

我在这里尝试了解决方案powershell v2 remoting - How do you enable unencrypted traffic,没有运气。

3 个答案:

答案 0 :(得分:2)

抱歉,长时间挣扎,不断改变可能的组合,最后使用它:

AuthenticationMechanism应该是AuthenticationMechanism.Default,而不是AuthenticationMechanism.Basic(这很奇怪)。

最终的工作版本是:

        SecureString password = new SecureString();
        string runasUsername = "USERNAME";
        string runasPassword = "PASSWORD";

        string liveIdconnectionUri = "http://EXCHANGE_SERVER/PowerShell";

        foreach (char x in runasPassword)
        {
            password.AppendChar(x);
        }

        PSCredential credential = new PSCredential(runasUsername, password);

        // Set the connection Info
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
        credential);

        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default; //AuthenticationMechanism.Default;

        // create a runspace on a remote path
        // the returned instance must be of type RemoteRunspace

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShell powershell = PowerShell.Create();
        PSCommand command = new PSCommand();

        command.AddCommand("Enable-Mailbox");
        command.AddParameter("Identity", "MAIL_USER_ID_HERE");

        powershell.Commands = command;
        try
        {
            // open the remote runspace
            runspace.Open();
            // associate the runspace with powershell
            powershell.Runspace = runspace;
            // invoke the powershell to obtain the results
            var result = powershell.Invoke();
            if (result.Count > 0)
                Console.WriteLine("sucessful!");
            else
                Console.WriteLine("failed!");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            // dispose the runspace and enable garbage collection
            runspace.Dispose();
            runspace = null;
            // Finally dispose the powershell and set all variables to null to free
            // up any resources.
            powershell.Dispose();
            powershell = null;
        }

        Console.WriteLine("done");
        Console.Read();

答案 1 :(得分:1)

我有同样的问题。还应该指出,对于托管PowerShell实例的EXCHANGE_SERVER上的虚拟目录,您应该在IIS管理器中将 SSL设置配置为“接受”而不是“需要SSL”,假设您仍然在服务器上安装默认的自签名证书。加上“ AuthenticationMechanism.Default ”设置摆脱了我在线上遇到的无数例外情况:

runspace.Open();

此外,如果您想在本地进行单元测试,则应该在桌面上Install the Exchange Management Tools

...或者,如果您没有Windows 8,请尝试以下方法:PowerShell Managed code in Exchange 2010

答案 2 :(得分:0)

AuthenticationMechanism.Default为我工作但导致另一条错误消息......

  

WinRM客户端无法处理请求。默认验证   可以在以下条件下使用IP地址:   传输是HTTPS或目标位于TrustedHosts列表中,并且   提供了显式凭证。使用winrm.cmd进行配置   TrustedHosts。请注意,TrustedHosts列表中的计算机可能不会   被认证。有关如何设置TrustedHosts运行的更多信息   以下命令:winrm help config。有关更多信息,请参阅   about_Remote_Troubleshooting帮助主题。

请注意,EXCHANGE_SERVER必须是DNS名称,而不是我正在使用的IP地址。我还必须在客户端和Exchange服务器上设置AllowUnencrypted配置设置。有关该设置的详细信息,请参阅以下链接。

powershell v2 remoting - How do you enable unencrypted traffic