2

我正在尝试使用 C# 中的 Enter-PSsession 来启用和使用 Powershell 远程处理来远程运行命令。这是一个代码

Runspace remoteRunspace = Runspaces.RunspaceFactory.CreateRunspace();
remoteRunspace.Open();
powershell.Runspace = remoteRunspace;
PSCommand command = new PSCommand();
command.AddCommand("Enter-PSSession");
command.AddParameter("ComputerName", "remotehostname");
command.AddParameter("Credential", vmmCredential);
powershell.Commands = command;
powershell.Invoke();

我得到 CmdletInvocationException。这是相同的详细信息。

System.Management.Automation.CmdletInvocationException 未处理 Message=方法或操作未实现。Source=System.Management.Automation WasThrownFromThrowStatement=false StackTrace:在 > System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(对象输入,哈希表错误结果,布尔枚举)在 System.Management.Automation.Internal.PipelineProcessor.SynchronousExecute(数组输入, Hashtable errorResults) 在 System.Management.Automation.Internal.PipelineProcessor.Execute(Array input) 在 System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper() 在 System.Management.Automation.Internal.PipelineProcessor.Execute() 在 System .Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc() InnerException:系统。Management.Automation.PSNotImplementedException Message=方法或操作未实现。Source=System.Management.Automation StackTrace:在 > System.Management.Automation.Internal.Host.InternalHost.GetIHostSupportsInteractiveSession() 在 > System.Management.Automation.Internal.Host.InternalHost.PushRunspace(Runspace runspace) 在 Microsoft.PowerShell。 System.Management.Automation.Cmdlet.DoProcessRecord() 的 Commands.EnterPSSessionCommand.ProcessRecord() 在 System.Management.Automation.CommandProcessor.ProcessRecord() InnerException:

如果我直接从 powershell 尝试 Enter-PSsession,我可以毫无问题地启动远程会话...任何人都可以就 C# 代码中的问题提出建议

4

1 回答 1

1

好的,我无法从 C# 代码中获取 Enter-PSSession cmdlet,但下面是我能够使用 C# 进行 PS 远程处理的代码...

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;

您现在可以使用 psh 远程运行命令。

于 2013-05-21T19:17:33.357 回答