user1156612
user1156612

Reputation: 41

how to establish and enter a remote session using runspace in powershell

I am trying to establish a session with a remote host B from my machine A, within a C# code. I am using runspace API for that. the code snippet is provided below

            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();

this code is expected to enter into a session with machine TS-TEST-09 and invoke the script helper.ps1 existing on that machine(that part is commented out in the code currently as i am not able to enter into the session with remote host).

now the problem is that i can't enter into the session $s using -Session parameter(highlighted at scripttext3) however i can enter into it using -Computername parameter.

the error that i get when using -Session parameter in scripttext3 is :

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

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

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

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

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

end of inner exception stack trace


does it mean i have to write a custom PSHost and add support for the Enter-PSSession cmdlet with this parameter?

Is there any alternative to make this command work?

any help will be much appreciated.

Thanks,

Manish

Upvotes: 2

Views: 9497

Answers (3)

Stig
Stig

Reputation: 87

I wanted to leave a comment on the solution, as it helped me alot as well,

but one thing i was missing was the port for WSMan which is 5985

so this var target = new Uri("http://myserver/wsman"); should be var target = new Uri("http://myserver:5895/wsman"); in my case

Upvotes: 1

x0n
x0n

Reputation: 52430

The easiest way to open a remote session goes something like this:

    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();
    }

You could also create remote pipelines from the remote runspace instance, but the new PowerShell object is a much more managable way to do this (since powershell v2.)

In powershell v3, you can just new up a WSManConnectionInfo and set the ComputerName property as the other properties adopt the same defaults as the above. Unfortunately these properties are read-only in v2 and you have to pass in the minimum as above. Other variants of the constructor will let you use kerberos/negotiate/credssp etc for authentication.

-Oisin

Upvotes: 8

stej
stej

Reputation: 29449

Have you tried Invoke-Command? Enter-PsSession is there for interactive use. (see help Enter-PsSession -online).

Upvotes: 0

Related Questions