2013-02-25 43 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

相關問題