2013-04-15 142 views
12

我想從我的電腦運行PowerShell代碼到VM我的電腦上失敗了,但我不斷收到此錯誤:連接到遠程服務器中使用WinRM的從PowerShell的

Connecting to remote server failed with the following error message : The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You can get more information about that by running the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.

我的代碼:

string runasUsername = @"\aaa"; 
    string runasPassword = "aaa"; 
    SecureString ssRunasPassword = new SecureString(); 
    foreach (char x in runasPassword) 
     ssRunasPassword.AppendChar(x); 
    PSCredential credentials = new PSCredential(runasUsername, ssRunasPassword); 

    var connInfo = new WSManConnectionInfo(new Uri("http://10.0.5.35/PowerShell"), 
     "http://schemas.microsoft.com/powershell/Microsoft.Exchange",credentials); 
    connInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; 

    var runspace = RunspaceFactory.CreateRunspace(connInfo); 


    var domainName = "domainName.COM"; 
    var password = "ActiveDirectoryPassword1234"; 
    var ssPassword = new SecureString(); 
    foreach (char c in password) 
     ssPassword.AppendChar(c); 


    var command = new Command("New-Mailbox"); 

    command.Parameters.Add("FirstName", firstName); 
    command.Parameters.Add("LastName", lastName); 
    command.Parameters.Add("Password", ssPassword); 
    command.Parameters.Add("ResetPasswordOnNextLogon", false); 
    command.Parameters.Add("OrganizationalUnit", "NeumontStudents"); 

    runspace.Open(); <--//error here 
    var pipeline = runspace.CreatePipeline(); 
    pipeline.Commands.Add(command); 


    var results = pipeline.Invoke(); 

    runspace.Dispose(); 

我錯過了什麼?

+0

你有沒有試圖檢查錯誤信息中提到的東西? –

回答

20

如果客戶機和遠程計算機都沒有在同一個域,你有兩個選擇之一:

  • 使用HTTPS作爲傳輸協議
  • 遠程計算機添加到列表中客戶

與可信任主機爲了configure WinRM使用HTTPS,開拓PowerShell控制檯爲兩臺機器管理員並運行:

winrm quickconfig -transport:https 

和開放端口5986在防火牆上:

netsh firewall add portopening TCP 5986 "WinRM over HTTPS" 

或者,您也可以通過運行在客戶端上添加遠程機器作爲可信主機

winrm set winrm/config/client '@{TrustedHosts="10.0.5.35"}' 
+0

是的,我得到這個:「WinRM已經被設置爲在這臺機器上接收請求。 WinRM已經被設置爲在這臺機器上進行遠程管理。」 – woolford

+0

@woolford是同一個域上的客戶端和服務器嗎? –

+0

Enrico Campidoglio no sir – woolford

1

你在這兩臺機器上啓用了winrm嗎? 嘗試在每臺計算機上運行winrm quickconfig以確保啓用遠程連接。

+0

是的,我得到這個:「WinRM已經被設置爲接收這臺機器上的請求。 WinRM已經被設置爲在這臺機器上進行遠程管理。」 – woolford

+0

嘗試使用CredSSP身份驗證。看在我的答案的步驟: http://stackoverflow.com/questions/15336336/running-batch-file-on-remote-computers-using-powershell-2-0/15336790#15336790 –

+0

的CredSSP用於允許遠程機器在連接到其他服務時傳遞客戶端憑證(例如,[第二跳](http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/14/enable-powershell- QUOT-第二跳-QUOT的功能與 - credssp.aspx))。這種情況下的問題是客戶端和服務器不在同一個域中。 –

相關問題