Managing multiple remote powershell sessions

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?

$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.

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


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

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.

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?

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.”

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 :slight_smile: Cheers

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.

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 :slight_smile: . I’ll be sure to check out the ebooks. This site is a great resource, glad I found it!