windows 在 C# 中创建远程 powershell 会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3216594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create remote powershell session in c#?
提问by noctonura
I'm very new to PS so I think I'm missing something basic here. I can do a remote powershell session like this...
我对 PS 很陌生,所以我想我在这里缺少一些基本的东西。我可以像这样进行远程 powershell 会话...
$Session = New-PSSession -ConfigurationName Microsoft.Exchange
-ConnectionUri https://remote.com/Powershell
-Credential "Domain\Admin"
I want the equivalent in C# so I'm trying this but it doesn't work...
我想要 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;
}
}
This fails with...
这失败了...
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....
How can I translate that into C#? What is the "shellUri" parameter and how to I convey the configuration name (Microsoft.Exchange in my case)?
我怎样才能把它翻译成 C#?什么是“shellUri”参数以及如何传达配置名称(在我的情况下为 Microsoft.Exchange)?
采纳答案by Taylor Bird
should work for shell uri and get context into the Exchange config
应该适用于 shell uri 并将上下文获取到 Exchange 配置中
回答by Vic
I use Something like
我使用类似的东西
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;
With this you have access to remote powershell session.. Use psh to run commands remotely.
有了这个,您就可以访问远程 powershell 会话。使用 psh 远程运行命令。