使用Enter-PSsession从c#远程访问Powershell v3



我试图从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未处理消息=该方法或操作未执行。源= System.Management.AutomationWasThrownFromThrowStatement = false加:System.Management.Automation.Internal.PipelineProcessor。SynchronousExecuteEnumerate(对象输入,哈希表errorResults,布尔枚举)在System.Management.Automation.Internal.PipelineProcessor。synchronousexecexecute(数组输入,Hashtable errorResults)在System.Management.Automation.Internal.PipelineProcessor。执行(数组输入)在System.Management.Automation.Internal.PipelineProcessor.Execute ()在System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper ()在System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc ()InnerException: System.Management.Automation.PSNotImplementedException消息=该方法或操作未执行。源= System.Management.Automation加:at> System.Management.Automation.Internal.Host.InternalHost.GetIHostSupportsInteractiveSession()在>System.Management.Automation.Internal.Host.InternalHost。PushRunspace (Runspace Runspace)在Microsoft.PowerShell.Commands.EnterPSSessionCommand.ProcessRecord ()在System.Management.Automation.Cmdlet.DoProcessRecord ()在System.Management.Automation.CommandProcessor.ProcessRecord ()InnerException:

如果我尝试直接从powershell进入- pssession,我可以启动一个远程会话没有问题…有谁能告诉我c#代码中有什么问题吗

OK,我无法从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远程运行命令

最新更新