在 C# 中创建远程 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”参数以及如何传达配置名称(在我的例子中为 Microsoft.Exchange)?
I'm very new to PS so I think I'm missing something basic here. I can do a remote powershell session like this...
$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...
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该适用于shell uri 并将上下文获取到 Exchange 配置中
should work for shell uri and get context into the Exchange config
我使用类似的东西
,您可以访问远程powershell会话。使用psh远程运行命令。
I use Something like
With this you have access to remote powershell session.. Use psh to run commands remotely.
shellUri 应为 http://schemas.microsoft.com/powershell/Microsoft。交换。
The shellUri should be http://schemas.microsoft.com/powershell/Microsoft.Exchange.