繁体   English   中英

在c#中创建远程PowerShell会话?

[英]Create remote powershell session in c#?

我对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)?

我用的东西就像

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM