C#创建远程PowerShell会话? [英] Create remote powershell session in c#?

查看:680
本文介绍了C#创建远程PowerShell会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的PS,所以我觉得我失去了一些东西基本在这里。我可以做一个远程PowerShell会话这样的...

  $会话=新的PSSession -ConfigurationName Microsoft.Exchange 
-ConnectionUri https://remote.com/Powershell
-CredentialDomain\Admin

我想在C#中的等价物,所以我想这一点,但它不工作...

  WSManConnectionInfo connInfo =新WSManConnectionInfo(
新的URI(https://remote.com/Powershell)/ * * URI /
/ WTF/ * shellUri ??? * /
CRED) / * PS-凭证* / $使用B
$ B(运行空间运行空间= RunspaceFactory.CreateRunspace(connInfo))
{
runspace.Open();使用(PowerShell的PS = PowerShell.Create())
{
ps.Runspace =运行空间
;
}
}

这失败...



  System.Management.Automation.Remoting.PSRemotingTransportException了未处理
消息=连接到具有以下错误消息失败的远程服务器:该WS-管理服务无法处理请求。资源的URI(/ WSMAN)在WS-管理目录中未找到......



如何我可以翻译成C#?什么是shellUri参数,以及如何我转达配置名称(Microsoft.Exchange在我的情况)?


解决方案

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




应该工作壳uri和获取上下文到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)?

解决方案

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

should work for shell uri and get context into the Exchange config

这篇关于C#创建远程PowerShell会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆