在 C# 中创建远程 powershell 会话?

发布于 2024-09-09 08:14:17 字数 1174 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我爱人 2024-09-16 08:14:17

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

应该适用于shell uri 并将上下文获取到 Exchange 配置中

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

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

心奴独伤 2024-09-16 08:14:17

我使用类似的东西

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文