1

I'm having a hard time understanding persistent connections in PowerShell during remoting sessions. I created my persistent connection and created a variable to test its lifespan:

$session = new-pssession server1
enter-pssession -session $session
$myvar = "abc"

Now I close the shell window to simulate a failure on my computer or reboot. I open up a new shell and try to get the connection from server1 with:

get-pssession server1

It comes up empty. How am I supposed to get back into my previous session?
Supposedly persistent connections are meant to withstand a disconnect or failure and should be available from your computer or even another computer but I can't make it work. What am I doing wrong?

4

All sessions that you create live in your local Windows PowerShell session, so when you close Windows PowerShell, all remaining session objects are discarded automatically. Session objects were not designed to be cross session persistent.

  • Thanks for answering Jim. I agree that the session is closing once you exit the shell after seeing it first hand myself. However, there are references online that say it should not. Especially if you use the disconnect-pssession cmdlet. I've tried using that cmdlet and still no session after closing the shell. See the section on disconnecting pssessions: windowsitpro.com/powershell/powershell-basics-remote-management – Jon Apr 29 '15 at 11:18
  • I would only add that if you need to continue operations after a reboot you should look at powershell workflows. – Colyn1337 Apr 29 '15 at 13:28
  • Thanks for the info Colyn. I'm just really trying to put into practice what I read. I don't necessarily need it at this moment would like to know that I can continue my session if I ever have to reboot. – Jon Apr 30 '15 at 20:27
  • If you wanted a session that you could later connect into - then why would you use enter-possession? Since you have created a temporary session why are you surprised it doesn't persist past shell shutdown? Actually read the article I linked to for how to create a session or at least read the powershell help on sessions – Jim B Apr 30 '15 at 20:53
  • Jim you didn't link any article. Also the new-pssession creates the persistent connection. You interact with it either with enter-pssession or invoke-command. If you do an exit-pssession it still remains alive. If you look at the comment I made for Zach you'll see the actual excerpt from the article I'm referring to about exiting the shell and still getting to it later. – Jon May 1 '15 at 14:12
1

Jim is right. The article you linked said that if you lost network connectivity, you could resume the operation when you get it back. Persistent in this case means that it keeps the session open after the command has finished. Without the New-Session command, Powershell cleans up connections automatically when they finish their task. The instructions you are following are for keeping the session open as long as you are running your shell.

  • Hi Zach. Below is the part that states you can shutdown your pc which essentially closes the shell session and come back to it. I mentioned this in my reply to Jim. While the scripts are running, your system tells you that it's about to shut down to perform an update. If you run a Disconnect-PSSession command before the shutdown occurs, the state of the PSSessions will be maintained on the remote computers. After the update, you can reboot your system and run a Connect-PSSession command to re-establish the PSSession. In the interim, work will continue on those remote systems. – Jon Apr 30 '15 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.