Getting all open PS Sessions on a remote server (f

2020-06-09 08:12发布

问题:

I can start 5 new PS sessions on a remote server and see them all by running Get-PSSession

PS C:\> New-PSSession -ComputerName MyServerName

     Id Name            ComputerName    State         ConfigurationName     Availability
     -- ----            ------------    -----         -----------------     ------------
      1 Session1        MyServerName   Opened        Microsoft.PowerShell     Available

    [repeat 4 more times]

As expected, when I try to open a 6th session, I get the error saying that's a no-no (due to PoswerShells default limit of 5 concurrent remote PSSessions). But running Get-Session shows all 5 sessions so all is working as it should be so far:

PS C:\> New-PSSession -ComputerName MyServerName
    New-PSSession : [......maximum number of 5 concurrent shells]

PS C:\> Get-PSSession

     Id Name            ComputerName    State         ConfigurationName     Availability
     -- ----            ------------    -----         -----------------     ------------
      1 Session1        MyServerName   Opened        Microsoft.PowerShell     Available
      2 Session2        MyServerName   Opened        Microsoft.PowerShell     Available
      3 Session3        MyServerName   Opened        Microsoft.PowerShell     Available
      4 Session4        MyServerName   Opened        Microsoft.PowerShell     Available
      5 Session5        MyServerName   Opened        Microsoft.PowerShell     Available

However, when I close that console and open a new one, running Get-PSSession (with or without the '-ComputerName' parameter defined) shows no open sessions at all.

PS C:\> Get-PSSession
PS C:\>

I know those sessions are still open because when I try to open a new one in my new console I get the same error regarding more than 5 concurrent sessions:

PS C:\> New-PSSession -ComputerName MyServerName
    New-PSSession : [......maximum number of 5 concurrent shells]

According to 'Get-PSSession Get-Help -full' running 'Get-PSSession -ComputerName MyServerName should get all remote PS sessions on a particular server regardless of what session or computer they were started from (at least the way I understand it):

"The command returns all of the sessions on [the remote server], even if they were created in different sessions or on different computers."

So, is there a way to find and/or remove any open PS sessions on a remote server -- without having to do it all from one console session?

回答1:

From what I can tell...

The PSSessions you created live on "MyServerName" and, for the duration of the session you first created them, will also be returned by Get-PSSession (with no arguments, in the PowerShell window on the box you're remoting FROM). When you close the session they are created in, the sessions are no longer on your computer. This is why Get-PSSession doesn't return anything when you close and open a new PowerShell window. They never "lived" on your computer, they are remote sessions, however, they were in scope in your original PowerShell window because that is the local scope you created them in.

If your sessions are still on MyServerName, as it seems they are due to the error you mention about max sessions, then typing the following command should list them:

Get-PSSession -ComputerName MyServerName

If you wanted to reconnect them all in your existing session/window, you could do:

Get-PSSession -ComputerName MyServerName | Connect-PSSession

To remove them all, enabling you to create new PSSessions to MyServerName

Get-PSSession -ComputerName MyServerName | Remove-PSSession

Looking a bit further in the docs, all sessions do not live indefinitely when you close your PowerShell window. See:

Get-Help about_Remote_Disconnected_Sessions -ShowWindow

partial excerpt (with emphasis mine):

If you close (exit) the session in which a PSSession was created while commands are running in the PSSession, Windows PowerShell maintains the PSSession in the Disconnected state on the remote computer. If you close (exit) the session in which a PSSession was created, but no commands are running in the PSSession, Windows PowerShell does not attempt to maintain the PSSession.

From what I can see, sessions that aren't a) disconnected, or b) busy running a command, are discarded when you close the PowerShell window you started the PSSessions from. Additionally, the documentation does seem to mention there are also timeouts (which probably depend on PSSessionConfigurations on the server, but I don't know anything about those yet myself (other than they exist).

This was a good excuse for me to sift through some of the PowerShell Remoting documentation, also look at:

Get-Help *PSSession*
Get-Help *remote*


回答2:

From my experimentation, if the sessions aren't doing anything then they get closed on the remote end. To prevent that, either have them do something e.g.:

Invoke-Command -Session $s { ... } -AsJob
Invoke-Command server01 { ... } -Disconnected

Or the other option is to disconnect your sessions:

Disconnect-PSSession -Id (1..5)

Both approaches will result in the remote sessions staying alive.



回答3:

Having this same issue today I came across this nice set of functions from jrich. I even simply pasted the functions into my PS window and ran

"<computername>" | Get-RemotePSSession | Remove-RemotePSSession

and voila! no more left-open sessions on said computer.

Here's the direct link to his blog. https://jrich523.wordpress.com/2012/01/19/managing-remote-wsman-sessions-with-powershell/#comment-1079