2013-02-25 133 views
2

以下代码运行:的PowerShell远程命令失败,因为未加密流量的

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

回答

2

对不起,挣扎了很久,不停地变化着可能的组合,最后用这个作品:

AuthenticationMechanismAuthenticationMechanism.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

基本是明文(未加密)认证类似的,当你得到一个网站上popupbox来。 WS-man的默认值是在域中使用Kerberos(加密)或在非域计算机之间进行协商。 – 2013-02-25 07:26:00

+0

谢谢格雷默。 – unruledboy 2013-03-02 13:23:24

1

我有同样的问题。还应该指出的是,对于托管PowerShell实例的EXCHANGE_SERVER上的虚拟目录,假定您仍然具有默认的自我管理功能,则应该将SSL设置配置为“接受”,但不将IIS管理器中的“要求SSL”签署的证书安装在服务器上。这再加上“AuthenticationMechanism.Default”设置摆脱了我在该行中遇到无数的异常:

runspace.Open(); 

另外,如果你想进行单元测试这个地方,你应该在桌面上Install the Exchange Management Tools

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

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