2013-11-20 112 views

回答

2

這段代碼顯示如何使用你想要的任何憑據打開的WSMan連接到遠程計算機:

PSCredential credential = new PSCredential(someUserName, someSecurePassword); 

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://targetmachinename:5985/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", credential); 
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate; 

using(Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))   
{ 
    ... use runspace here ... 
} 

現在,將連接了一個最基本的PowerShell的實例。如果你想連接到Exchange準備PowerShell的情況下,你可以指定Exchange shellUri而不是WSManConnectionInfo,你甚至不應該擔心添加單元:

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://targetmachinename:5985/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential); 
+0

原諒我,如果這是一個愚蠢題。我明白我的URI是什麼,但我如何確定shell的URI? – Johnrad

+0

我會更新答案。 –

+0

如何確定此交換shell的URI? – Johnrad