2010-07-09 99 views
3

我對PS很新,所以我想我在這裏缺少一些基本的東西。我可以做這樣的遠程PowerShell會話...在C#中創建遠程PowerShell會話?

$Session = New-PSSession -ConfigurationName Microsoft.Exchange 
          -ConnectionUri https://remote.com/Powershell 
          -Credential "Domain\Admin" 

我想在C#中相當於所以我想這一點,但它不工作...

WSManConnectionInfo connInfo = new WSManConnectionInfo(
    new Uri("https://remote.com/Powershell"), /* uri */ 
    "/wtf",      /* shellUri??? */ 
    cred);          /* ps-credential */ 

using (Runspace runspace = RunspaceFactory.CreateRunspace(connInfo)) 
{ 
    runspace.Open(); 
    using (PowerShell ps = PowerShell.Create()) 
    { 
     ps.Runspace = runspace; 
    } 
} 

這種失敗...

System.Management.Automation.Remoting.PSRemotingTransportException was unhandled 
    Message=Connecting to remote server failed with the following error message : The WS-Management service cannot process the request. The resource URI (/wsman) was not found in the WS-Management catalog.... 

如何將它轉換爲C#?什麼是「shellUri」參數,以及如何傳達配置名稱(在我的情況下是Microsoft.Exchange)?

回答

1

我使用類似

int iRemotePort = 5985; 
string strShellURI = @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell"; 
string strAppName = @"/wsman"; 
AuthenticationMechanism auth = AuthenticationMechanism.Negotiate; 

WSManConnectionInfo ci = new WSManConnectionInfo(
    false, 
    sRemote, 
    iRemotePort, 
    strAppName, 
    strShellURI, 
    creds); 
ci.AuthenticationMechanism = auth; 

Runspace runspace = RunspaceFactory.CreateRunspace(ci); 
runspace.Open(); 

PowerShell psh = PowerShell.Create(); 
psh.Runspace = runspace; 

有了這個,你可以訪問遠程PowerShell會話..使用PSH遠程運行命令。

相關問題