2

以下代码运行:

        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,没有运气。

4

3 回答 3

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();
于 2013-02-25T01:02:21.197 回答
1

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

runspace.Open();

此外,如果您想在本地对此进行单元测试,您应该在桌面上安装 Exchange 管理工具。

...或者,如果您没有 Windows 8,请尝试以下方法: Exchange 2010 中的 PowerShell 托管代码

于 2013-09-19T19:27:43.950 回答
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 远程处理 - 如何启用未加密的流量

于 2016-04-20T18:33:00.553 回答