在C#中创build远程PowerShell会话?

我对PS很陌生,所以我想我在这里错过了一些基本的东西。 我可以做这样的远程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”参数,以及如何传达configuration名称(在我的情况下是Microsoft.Exchange)?

http://schemas.microsoft.com/powershell/Microsoft.Exchange

应该为shell uri工作,并获取上下文到Exchange配置

我使用类似的东西

 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远程运行命令。