如何在PowerShell中使用运行空间建立和进入远程会话

use*_*612 2 powershell pshost runspace

我正在尝试在C#代码中从我的机器A与远程主机B建立会话.我正在使用runspace API.下面提供了代码段

            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();

            //constructing the vmname parameter here
            vmname = useralias + DateTime.Now.ToString();


            Pipeline pipeline = runspace.CreatePipeline();
            string scripttext = "$secpasswd = ConvertTo-SecureString '222_bbbb' -AsPlainText –Force";
            string scripttext1 = "$mycreds = New-object -typename System.Management.Automation.PSCredential('TS-TEST-09\\Administrator',$secpasswd)";
            string scripttext2  = "$s = New-PSSession -ComputerName TS-TEST-09 -Credential $mycreds";
            //not accepting session string here, only computername acceptable
            **string scripttext3 = "Enter-PSSession -Session $s";**



            //Command cmd = new Command(@"C:\mypath\helper.ps1", true);
            //cmd.Parameters.Add("local_useralias", useralias);
            //cmd.Parameters.Add("local_vmname", vmname);
            //cmd.Parameters.Add("local_datastore", datastoredropdown.Text.ToString());
            //cmd.Parameters.Add("local_architecture", architecturedropdown.Text.ToString());


            pipeline.Commands.AddScript(scripttext);
            pipeline.Commands.AddScript(scripttext1);
            pipeline.Commands.AddScript(scripttext2);
            pipeline.Commands.AddScript(scripttext3);
            //pipeline.Commands.Add(cmd);


            Collection<PSObject> results = pipeline.Invoke();


            runspace.Close();
Run Code Online (Sandbox Code Playgroud)

这个代码应该进入与机器TS-TEST-09的会话并调用该机器上存在的脚本helper.ps1(该部分在代码中被注释掉,因为我无法进入与远程主机的会话).

现在问题是我无法使用-Session参数(在scripttext3处突出显示)进入会话$ s但是我可以使用-Co​​mputername参数进入它.

在scripttext3中使用-Session参数时得到的错误是:

at invokedSystem.Management.Automation.Internal.Host.InteralHost.GetIHostSupportsInteractiveSession()

在invokedSystem.Management.Automation.Internal.Host.InteralHost.PushRunspace(Runspace runspace)

在Microsoft.Powershel.Commands.EnterPSSessionCommand.ProcessRecord()

在System.Management.Automation.Cmdlet.DoProcessRecord()

在System.Management.Automation.CommandProcessor.ProcessRecord()

内部异常堆栈跟踪的结束


这是否意味着我必须编写自定义PSHost并使用此参数添加对Enter-PSSession cmdlet的支持?

有没有其他方法可以使这个命令工作?

任何帮助都感激不尽.

谢谢,

马尼什

x0n*_*x0n 7

打开远程会话的最简单方法是这样的:

    string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
    var target = new Uri("http://myserver/wsman");
    var secured = new SecureString();
    foreach (char letter in "mypassword")
    {
        secured.AppendChar(letter);
    }
    secured.MakeReadOnly();

    var credential = new PSCredential("username", secured);
    var connectionInfo = new WSManConnectionInfo(target, shell, credential);

    Runspace remote = RunspaceFactory.CreateRunspace(connectionInfo);
    remote.Open();

    using (var ps = PowerShell.Create())
    {
        ps.Runspace = remote;
        ps.Commands.AddCommand("'This is running on {0}.' -f (hostname)");

        Collection<PSObject> output = ps.Invoke();
    }
Run Code Online (Sandbox Code Playgroud)

您还可以从远程运行空间实例创建远程管道,但新PowerShell对象是一种更易于管理的方法(因为PowerShell v2.)

在powershell v3中,您可以新建一个WSManConnectionInfo并设置该ComputerName属性,因为其他属性采用与上面相同的默认值.不幸的是,这些属性在v2中是只读的,你必须传递最小值,如上所述.构造函数的其他变体将允许您使用kerberos/negotiate/credssp等进行身份验证.

-Oisin