关于powershell:在远程服务器上获取所有打开的PS会话(从新的控制台窗口)

Getting all open PS Sessions on a remote server (from new console window)

我可以在远程服务器上启动5个新的PS会话,并通过运行Get-PSSession

查看所有会话

1
2
3
4
5
6
7
PS C:\\> New-PSSession -ComputerName MyServerName

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

    [repeat 4 more times]

正如预期的那样,当我尝试打开第6个会话时,我得到一个错误,说这是不可以的(由于PoswerShells默认限制为5个并发远程PSSession)。但是运行Get-Session会显示所有5个会话,因此到目前为止一切正常:

1
2
3
4
5
6
7
8
9
10
11
12
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

但是,当我关闭该控制台并打开一个新控制台时,运行Get-PSSession(定义或不定义'-ComputerName'参数)根本没有打开的会话。

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

我知道那些会话仍处于打开状态,因为当我尝试在新控制台中打开一个新会话时,关于超过5个并发会话,我会收到相同的错误:

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

根据" Get-PSSession Get-Help -full",运行" Get-PSSession -ComputerName MyServerName"应获取特定服务器上的所有远程PS会话,而不管它们是从哪个会话或计算机启动的(至少是我了解的方式)它):

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

因此,是否有一种方法可以查找和/或删除远程服务器上的所有打开的PS会话-无需从一个控制台会话中全部完成操作?


据我所知...

您在" MyServerName"上实时创建的PSSession,并且在您首次创建会话的过程中,Get-PSSession也将返回该PSSession(不带任何参数,在您要从中进行远程处理的PowerShell窗口中) )。当您关闭创建会话时,这些会话将不再在您的计算机上。这就是为什么当您关闭并打开一个新的PowerShell窗口时,Get-PSSession不返回任何内容的原因。它们从不"驻留"在您的计算机上,它们是远程会话,但是它们在原始PowerShell窗口的作用域内,因为这是您在其中创建它们的本地作用域。

如果您的会话仍在MyServerName上,则似乎是由于您提到的有关最大会话的错误所致,然后键入以下命令应将其列出:

1
Get-PSSession -ComputerName MyServerName

如果要在现有的会话/窗口中重新连接它们,可以执行以下操作:

1
Get-PSSession -ComputerName MyServerName | Connect-PSSession

要全部删除它们,使您可以创建到MyServerName的新PSSession

1
Get-PSSession -ComputerName MyServerName | Remove-PSSession

再仔细看看文档,当您关闭PowerShell窗口时,所有会话都不会无限期地存在。参见:

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.

据我所见,当关闭启动PSSessions的PowerShell窗口时,没有a)断开连接或b)忙于运行命令的会话将被丢弃。此外,文档似乎确实提到了超时(这可能取决于服务器上的PSSessionConfigurations,但是我自己还不知道这些超时(不存在)。

这是我浏览一些PowerShell Remoting文档的好借口,另请参阅:

1
2
Get-Help *PSSession*
Get-Help *remote*


根据我的实验,如果会话没有执行任何操作,则它们将在远端关闭。为了防止这种情况发生,请让他们做一些事情,例如:

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

或者另一个选择是断开会话:

1
Disconnect-PSSession -Id (1..5)

这两种方法都会导致远程会话保持活动状态。


今天遇到同样的问题,我遇到了来自jrich的一组不错的函数。
我什至只是简单地将函数粘贴到我的PS窗口中,然后运行

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

瞧!所述计算机上不再有打开的会话。

这是他博客的直接链接。
https://jrich523.wordpress.com/2012/01/19/managing-remote-wsman-sessions-with-powershell/#comment-1079