Managing multiple remote powershell sessions

This topic contains 9 replies, has 3 voices, and was last updated by Profile photo of Tom Vernon Tom Vernon 4 months, 2 weeks ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #38820
    Profile photo of Tom Vernon
    Tom Vernon
    Participant

    Hi there, I'm hoping for some help with managing multiple persistent remote powershell sessions within a script. I currently have one session which connects to O365 via Import-PSSession, then another session which connects to a server on my network via Enter-PSSession. I need to be able to "dip in" to each of these sessions multiple times within my script. Would someone be able to provide a useful example of how I manage the multiple sessions(connect/disconnect) to them multiple times?

    #38861
    Profile photo of Don Jones
    Don Jones
    Keymaster

    $o365 = New-PSSession ...
    $local = New-PSSession ...

    Enter-PSSession $local

    So, save the sessions in variables. You can then refer to them whenever you need to reuse that session. Be aware that the O365 session will have a relatively shorter timeout than the local one, so you may need to implement some checking to see if the session is available and ready before using it.

    #38917
    Profile photo of Tom Vernon
    Tom Vernon
    Participant

    Thanks Don, I was struggling with the correct pssession cmdlet to use as they all have subtle differences. So am I correct in saying that I can use the enter and exit cmdlets multiple times to dip in and out of the session and then use remove at the end of the script?:

    Enter-PSSession $local
    do something
    Exit-PSSession $local

    Enter-PSSession $O365
    do something else
    Exit-PSSession $O365

    Enter-PSSession $local
    do something else
    Exit-PSSession $local

    Remove-PSSession $local
    Remove-PSSession $O365

    Thanks

    #38920
    Profile photo of Henry Buckle
    Henry Buckle
    Participant

    I don't believe you can use Enter-PSSession in a script, it only works as an interactive command. You would want to use Invoke-Command with the -Session parameter

    #38930
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So, you can't use Enter-PSSession to direct commands from your script into the remote computer, no. As Henry points out, you'd use Invoke-Command, and reference the existing session rather than a computer name.

    #38933
    Profile photo of Tom Vernon
    Tom Vernon
    Participant

    Understood gents. Ive seen some scripted examples using the Import-PSSession cmdlet. Would this be a potential alternative or is invoke-command my only option? It doesn't seem very efficient calling invoke-command several times?

    #38935
    Profile photo of Don Jones
    Don Jones
    Keymaster

    You should probably look into what Import-PSSession actually does... I'm not sure if you're aware of it's function. Happy to lay it out for you, if you like. But, under the hood, what Import-PSSession ends up doing (via the proxy module it creates) is calling Invoke-Command over and over. Invoke-Command is the only way to make a remote computer do something for you. Under the hood, Enter-PSSession is just using Invoke-Command, really. It isn't like an SSH session.

    If you're wondering, "isn't there a way I can just 'switch my script' so that everything is running remotely, then switch back," the answer is, "no, that isn't how Remoting is architected at all."

    #38936
    Profile photo of Tom Vernon
    Tom Vernon
    Participant

    Thanks for the comprehensive answer Don. I'm a Bash scripter traditionally so on a bit of a learning curve. invoke-command it is then 🙂 Cheers

    #38937
    Profile photo of Don Jones
    Don Jones
    Keymaster

    That's a tough angle to come from, because PowerShell looks so much like it and is so, so not like it.

    In Remoting, we always send a command or script block to a remote computer. This happens with Enter-PSSession, too – your typing occurs locally; it isn't until you hit Enter that it's bundled and sent to the remote. The remote runs the command, and the resulting objects are serialized into XML. That XML is transmitted to you, where PowerShell deserializes them back into objects.

    Import-PSSession actually creates a local "proxy module" that mimics a remote module. The proxy lets your local console "see" the command and its parameters and help. But, when you run one of those commands, it's actually Invoke-Command'ed to the remote computer for execution, serialization, transmission, deserialization as described above.

    So it's nothing like SSH, really, or Telnet or anything else. It's almost a REST web service to a large degree (except it's more like SOAP). There's no "interactive" remote session.

    So... more background than you asked for ;). We've got a great free ebook, "Secrets of PowerShell Remoting," on the Resources menu if you'd like to dig more.

    #38938
    Profile photo of Tom Vernon
    Tom Vernon
    Participant

    Fantastic explanation thankyou. At the same time I'm also currently writing Ansible scripts which essentially execute python and bash code over SSH sessions, so your analogy is very appropriate and clears up some confusion 🙂 . I'll be sure to check out the ebooks. This site is a great resource, glad I found it!

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.