about_Remote_Disconnected_Sessions
简短说明
说明如何断开 PowerShell 会话的连接并重新连接到 PSSession) (。
长说明
从 PowerShell 3.0 开始,可以从 PSSession 断开连接,并从同一计算机或其他计算机重新连接到 PSSession。 会话状态得到维护,PSSession 中的命令在会话断开连接时继续运行。
断开连接的会话功能允许关闭在其中创建 PSSession 的会话并关闭计算机,而不会中断远程 PSSession 中运行的命令。 断开连接的会话对于运行需要较长时间才能完成的命令非常有用。
无法从使用 Enter-PSSession
cmdlet 启动的交互式会话断开连接。
可以使用断开连接的会话来管理由于计算机或网络中断而意外断开连接的 PSSession。
断开连接的会话 cmdlet
以下 cmdlet 支持断开连接的会话功能:
Connect-PSSession
:连接到已断开连接的 PSSession。Disconnect-PSSession
:断开 PSSession 的连接。Get-PSSession
:获取本地计算机或远程计算机上的 PSSession。Receive-PSSession
:获取在断开连接的会话中运行的命令的结果。Invoke-Command
: InDisconnectedSession 参数创建 PSSession 并立即断开连接。
断开连接的会话功能的工作原理
从 PowerShell 3.0 开始,PSSession 独立于创建它们的会话。 活动 PSSession 在连接的远程计算机或 服务器端 维护,即使 客户端 计算机已关闭或与网络断开连接。
在 PowerShell 2.0 中,当 PSSession 与原始会话或创建它的会话结束断开连接时,会从远程计算机中删除 PSSession。
断开 PSSession 的连接时,PSSession 将保持活动状态,并保留在远程计算机上。 会话状态从 “正在运行” 更改为 “已断开连接”。 可以从 重新连接到断开连接的 PSSession
- 同一计算机上的当前会话
- 同一计算机上的另一个会话
- 从其他计算机上的会话
维护会话的远程计算机必须正在运行并连接到网络。
断开连接的 PSSession 中的命令将继续在远程计算机上不间断地运行,直到命令完成或输出缓冲区填充为止。 若要防止完整输出缓冲区挂起命令,请使用 、 New-PSSessionOption
或 New-PSTransportOption
cmdlet 的 Disconnect-PSSession
OutputBufferingMode 参数。
断开连接的会话在远程计算机上以断开连接状态进行维护。 在删除 PSSession(例如使用 Remove-PSSession
cmdlet)或 PSSession 的空闲超时过期之前,可以使用它们重新连接。 可以使用 、 New-PSSessionOption
New-PSTransportOption
或 cmdlet 的 IdleTimeoutSec 或 IdleTimeout 参数调整 PSSession 的Disconnect-PSSession
空闲超时。
另一个用户可以连接到你创建的 PSSession,但前提是他们可以提供用于创建会话的凭据,或使用 RunAs
会话配置的凭据。
如何获取 PSSession
从 PowerShell 3.0 开始, Get-PSSession
cmdlet 获取本地计算机和远程计算机上的 PSSession。 它还可以获取在当前会话中创建的 PSSession。
若要在本地计算机或远程计算机上获取 PSSession,请使用 ComputerName 或 ConnectionUri 参数。 如果没有参数, Get-PSSession
则获取在本地会话中创建的 PSSession,无论它们在何处终止。
下面的示例说明如何使用 Get-PSSession
。
New-PSSession
创建与 Server01 计算机的会话。 会话驻留在 Server01 计算机上。
New-PSSession -ComputerName Server01
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
2 Session2 Server01 Opened Microsoft.PowerShell Available
若要从 Server01 获取会话,请使用 ComputerName 参数指定 的目标 Get-PSSession
。
Get-PSSession -ComputerName Server01
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
2 Session2 Server01 Opened Microsoft.PowerShell Available
如果 的 ComputerName 参数 Get-PSSession
值为 localhost, Get-PSSession
则获取在本地计算机上终止并维护的 PSSession。 它不会在 Server01 计算机上获取 PSSession,即使它们是在本地计算机上启动的。
Get-PSSession -ComputerName localhost
若要获取在当前会话中创建的会话,请使用不带参数的 Get-PSSession
cmdlet。 在此示例中, Get-PSSession
获取在当前会话中创建并连接到 Server01 计算机的 PSSession。
Get-PSSession
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
2 Session2 Server01 Opened Microsoft.PowerShell Available
如何断开会话的连接
使用 Disconnect-PSSession
cmdlet 断开会话的连接。 若要标识 PSSession,请使用 Session 参数,或将 PSSession 对象从 New-PSSession
或 Get-PSSession
cmdlet 管道传递给 Disconnect-PSSession
。
以下命令断开 PSSession 与 Server01 计算机的连接。 请注意, State 属性的值为 Disconnected , 可用性 为 None。
Get-PSSession -ComputerName Server01 | Disconnect-PSSession
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
2 Session2 Server01 Disconnected Microsoft.PowerShell None
若要创建断开连接的会话,请使用 cmdlet 的 InDisconnectedSession 参数。Invoke-Command
它会创建会话,启动命令并立即断开连接,然后命令可以返回任何输出。
以下命令在远程计算机 Server02 上断开连接的会话中运行 Get-WinEvent
命令。
Invoke-Command -ComputerName Server02 -InDisconnectedSession -ScriptBlock {
Get-WinEvent -LogName "*PowerShell*" }
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
4 Session3 Server02 Disconnected Microsoft.PowerShell None
如何连接到断开连接的会话
若要连接断开连接的会话,请将 Connect-PSSession
cmdlet 与 ComputerName 或 ConnectionUri 参数一起使用。 或者,可以通过管道将 的 Get-PSSession
输出传递给 Connect-PSSession
。
以下示例获取 Server02 计算机上的会话。 输出包括两个断开连接的会话。
Get-PSSession -ComputerName Server02
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
2 Session2 juneb-srv8320 Disconnected Microsoft.PowerShell None
4 Session3 juneb-srv8320 Disconnected Microsoft.PowerShell None
以下命令连接到 Session2。 PSSession 现已打开并可用。
Connect-PSSession -ComputerName Server02 -Name Session2
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
2 Session2 juneb-srv8320 Opened Microsoft.PowerShell Available
如何获取结果
若要获取在断开连接的 PSSession 中运行的命令的结果,请使用 Receive-PSSession
cmdlet。
可以使用 Receive-PSSession
cmdlet 而不是 cmdlet Connect-PSSession
。 如果会话已重新连接, Receive-PSSession
则获取会话断开连接时运行的命令的结果。 如果 PSSession 仍然断开连接, Receive-PSSession
则连接到它,然后获取在断开连接时运行的命令的结果。
Receive-PSSession
可以将作业中的结果 (异步) 或 (同步) 返回给主机程序。 使用 OutTarget 参数选择 “作业 ”或“ 主机”。 默认值为 Host。 但是,如果收到的命令是作为 作业在当前会话中启动的,则默认情况下,该命令将作为 作业 返回。
以下示例使用 Receive-PSSession
cmdlet 重新连接到 Server02 上的会话,并获取命令的结果 Get-WinEvent
。 OutTarget 参数用于获取作业中的结果。
Receive-PSSession -ComputerName Server02 -Name Session3 -OutTarget Job
Id Name PSJobTypeName State HasMoreData Location
-- ---- ------------- ----- ----------- --------
3 Job3 RemoteJob Running True Server02
若要获取作业的结果,请使用 Receive-Job
cmdlet。
Get-Job | Receive-Job -Keep
ProviderName: PowerShell
TimeCreated Id LevelDisplayName Message PSComputerName
----------- -- ---------------- ------- --------------
5/14/2012 7:26:04 PM 400 Information Engine stat Server02
5/14/2012 7:26:03 PM 600 Information Provider "W Server02
5/14/2012 7:26:03 PM 600 Information Provider "C Server02
5/14/2012 7:26:03 PM 600 Information Provider "V Server02
状态和可用性属性
断开连接的 PSSession 的状态 和 可用性 属性指示会话是否可供你重新连接到该会话。
当 PSSession 连接到当前会话时,其状态为 “已打开” ,其可用性为 “可用”。 从 PSSession 断开连接时,PSSession 状态为 “已断开连接” ,其可用性为 “无”。
State 属性的值是相对于当前会话的。 如果值为 Disconnected ,则表示 PSSession 未连接到当前会话。 但是,这并不意味着 PSSession 与所有会话断开连接。 它可能连接到另一个会话。
若要确定是否可以连接或重新连接到 PSSession,请使用 Availability 属性。 值为 None 表示可以连接到会话。 如果值为 Busy ,则表示无法连接到 PSSession,因为它已连接到另一个会话。
以下示例在同一计算机上的两个 PowerShell 会话中运行。 请注意,当 PSSession 断开连接并重新连接时,请注意每个会话中 状态 和 可用性 属性值的变化。
# Session 1
New-PSSession -ComputerName Server30 -Name Test
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
1 Test Server30 Opened Microsoft.PowerShell Available
# Session 2
Get-PSSession -ComputerName Server30 -Name Test
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
1 Test Server30 Disconnected Microsoft.PowerShell Busy
# Session 1
Get-PSSession -ComputerName Server30 -Name Test | Disconnect-PSSession
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
1 Test Server30 Disconnected Microsoft.PowerShell None
# Session 2
Get-PSSession -ComputerName Server30
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
1 Test Server30 Disconnected Microsoft.PowerShell None
# Session 2
Connect-PSSession -ComputerName Server30 -Name Test
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
3 Test Server30 Opened Microsoft.PowerShell Available
# Session 1
Get-PSSession -ComputerName Server30
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
1 Test Server30 Disconnected Microsoft.PowerShell Busy
断开连接的会话将保留在远程计算机上,直到删除它们(例如使用 Remove-PSSession
cmdlet)或它们超时。PSSession 的 IdleTimeout 属性确定断开连接的会话在删除之前保留多长时间。
空闲超时值
当 检测信号线程 未收到响应时,PSSession 处于空闲状态。 断开会话的连接会使会话处于空闲状态并启动 IdleTimeout 时钟,即使命令仍在断开连接的会话中运行也是如此。 PowerShell 认为断开连接的会话处于活动状态,但处于空闲状态。
创建会话和断开连接会话时,请验证 PSSession 中的空闲超时时间是否足够长,以维持会话以满足需求,但不要太长,从而消耗远程计算机上的不必要的资源。
会话配置的 IdleTimeoutMs 属性确定使用会话配置的会话的默认空闲超时。 可以替代默认值,但该值不能超过会话配置的 MaxIdleTimeoutMs 属性。
使用以下命令获取会话配置的 IdleTimeoutMs 和 MaxIdleTimeoutMs 的值。
Get-PSSessionConfiguration |
Format-Table Name, IdleTimeoutMs, MaxIdleTimeoutMs
如果你是远程计算机上的管理员组的成员,则可以在创建会话配置时设置这些值。 此外,还可以在断开连接时更改值。
会话配置和会话选项的空闲超时值以毫秒为单位。 会话和会话配置选项的空闲超时值以秒为单位。
创建 PSSession (时, Invoke-Command
可以设置 PSSession 的New-PSSession
空闲超时,) () 断开连接Disconnect-PSSession
。 但是,连接到 PSSession (Connect-PSSession
) 或获取结果 Receive-PSSession
() 时,无法更改 IdleTimeout 值。
Connect-PSSession
和 Receive-PSSession
cmdlet 具有一个 SessionOption 参数,该参数采用 PSSessionOption 对象,例如 cmdlet 返回的New-PSSessionOption
一个对象。
SessionOption 对象中的 IdleTimeout 值和首选项变量中的 $PSSessionOption
IdleTimeout 值不会更改 或 Receive-PSSession
命令中 Connect-PSSession
IdleTimeout 的值。
若要创建具有特定空闲超时值的 PSSession,请创建首选项 $PSSessionOption
变量。 将 IdleTimeout 属性的值设置为所需的值, () 以毫秒为单位。
创建 PSSession 时,变量中的 $PSSessionOption
值优先于会话配置中的值。
例如,以下命令将空闲超时设置为 48 小时:
$PSSessionOption = New-PSSessionOption -IdleTimeoutMSec 172800000
若要创建具有特定空闲超时值的 PSSession,请使用 cmdlet 的 New-PSSessionOption
IdleTimeoutMSec 参数。 然后,在 或 Invoke-Command
cmdlet 的 SessionOption 参数New-PSSession
的值中使用会话选项。
创建会话时设置的值优先于首选项变量和会话配置中 $PSSessionOption
设置的值。
例如:
$o = New-PSSessionOption -IdleTimeoutMSec 172800000
New-PSSession -SessionOption $o
若要在断开连接时更改 PSSession 的空闲超时,请使用 cmdlet 的 Disconnect-PSSession
IdleTimeoutSec 参数。
例如:
Disconnect-PSSession -IdleTimeoutSec 172800
若要创建具有特定空闲超时和最大空闲超时的会话配置,请使用 cmdlet 的 New-PSTransportOption
IdleTimeoutSec 和 MaxIdleTimeoutSec 参数。 然后,在 的 TransportOption 参数 Register-PSSessionConfiguration
的值中使用传输选项。
例如:
$o = New-PSTransportOption -IdleTimeoutSec 172800 -MaxIdleTimeoutSec 259200
Register-PSSessionConfiguration -Name Test -TransportOption $o
若要更改会话配置的默认空闲超时和最大空闲超时,请使用 cmdlet 的 New-PSTransportOption
IdleTimeoutSec 和 MaxIdleTimeoutSec 参数。 然后,在 的 TransportOption 参数 Set-PSSessionConfiguration
的值中使用传输选项。
例如:
$o = New-PSTransportOption -IdleTimeoutSec 172800 -MaxIdleTimeoutSec 259200
Set-PSSessionConfiguration -Name Test -TransportOption $o
输出缓冲模式
PSSession 的输出缓冲模式确定当 PSSession 的输出缓冲区已满时如何管理命令输出。
在断开连接的会话中,输出缓冲模式可有效地确定在会话断开连接时命令是否继续运行。
有效值如下所示:
Block
(默认) - 输出缓冲区已满时,将暂停执行,直到清除缓冲区。Block
保留数据,但可能会中断命令。Drop
- 当输出缓冲区已满时,将继续执行。 生成新输出时,将丢弃最早的输出。 使用Drop
值时,将输出重定向到 文件。 对于断开连接的会话,建议使用此值。
会话配置的 OutputBufferingMode 属性确定使用会话配置的会话的默认缓冲模式。
若要查找 会话配置的 OutputBufferingMode 值,可以使用下列任一命令格式:
(Get-PSSessionConfiguration <ConfigurationName>).OutputBufferingMode
Get-PSSessionConfiguration | Format-Table Name, OutputBufferingMode
可以在创建 PSSession、断开连接和重新连接时替代会话配置中的默认值并设置 PSSession 的输出缓冲模式。
如果你是远程计算机上的管理员组的成员,则可以创建和更改会话配置的输出缓冲模式。
若要创建输出缓冲模式为 的 Drop
PSSession,请创建一个 $PSSessionOption
首选项变量,其中 OutputBufferingMode 属性 Drop
的值为 。
创建 PSSession 时,变量中的 $PSSessionOption
值优先于会话配置中的值。
例如:
$PSSessionOption = New-PSSessionOption -OutputBufferingMode Drop
使用 cmdlet 的 New-PSSessionOption
OutputBufferingMode 参数创建值为 的Drop
会话选项。 然后,使用 PSSessionOption 对象作为 或 Invoke-Command
cmdlet 的 New-PSSession
SessionOption 参数的值。
创建会话时设置的值优先于首选项变量和会话配置中 $PSSessionOption
设置的值。
例如:
$o = New-PSSessionOption -OutputBufferingMode Drop
New-PSSession -SessionOption $o
若要在断开连接时更改 PSSession 的输出缓冲模式,请使用 cmdlet 的 Disconnect-PSSession
OutputBufferingMode 参数。
例如:
Disconnect-PSSession -OutputBufferingMode Drop
若要在重新连接时更改 PSSession 的输出缓冲模式,请使用 cmdlet 的 New-PSSessionOption
OutputBufferingMode 参数。 然后,在 或 Receive-PSSession
的 SessionOption 参数Connect-PSSession
的值中使用会话选项。
例如:
$o = New-PSSessionOption -OutputBufferingMode Drop
Connect-PSSession -ComputerName Server01 -Name Test -SessionOption $o
若要创建默认输出缓冲模式为 的Drop
会话配置,请使用 cmdlet 的 New-PSTransportOption
OutputBufferingMode 参数创建值为 的Drop
传输选项对象。 然后,在 的 TransportOption 参数 Register-PSSessionConfiguration
的值中使用传输选项。
例如:
$o = New-PSTransportOption -OutputBufferingMode Drop
Register-PSSessionConfiguration -Name Test -TransportOption $o
若要更改会话配置的默认输出缓冲模式,请使用 cmdlet 的 New-PSTransportOption
OutputBufferingMode 参数创建值为 的Drop
传输选项。 然后,在 的 SessionOption 参数 Set-PSSessionConfiguration
的值中使用 Transport 选项。
例如:
$o = New-PSTransportOption -OutputBufferingMode Drop
Set-PSSessionConfiguration -Name Test -TransportOption $o
断开环回会话的连接
环回会话或本地会话是在同一台计算机上发起和终止的 PSSession。 与其他 PSSession 一样,活动环回会话保留在远程连接端的计算机上, (本地计算机) ,因此你可以断开环回会话的连接,并重新连接到环回会话。
默认情况下,环回会话是使用网络安全令牌创建的,该令牌不允许在会话中运行命令来访问其他计算机。 可以从本地计算机或远程计算机上的任何会话重新连接到具有网络安全令牌的环回会话。
但是,如果使用 、 Enter-PSSession
或 Invoke-Command
cmdlet 的 New-PSSession
EnableNetworkAccess 参数,则会使用交互式安全令牌创建环回会话。 交互式令牌使环回会话中运行的命令能够从其他计算机获取数据。
可以使用交互式令牌断开环回会话的连接,然后从同一计算机上的同一会话或不同会话重新连接到这些会话。 但是,为了防止恶意访问,只能从创建交互式令牌的计算机使用交互式令牌重新连接到环回会话。
在断开连接的会话中等待作业
cmdlet Wait-Job
将等待作业完成,然后返回到命令提示符或下一个命令。 默认情况下,如果运行作业的会话断开连接, Wait-Job
则返回 。 若要指示 Wait-Job
cmdlet 等待会话重新连接,请在 “已打开 ”状态下使用 Force 参数。 有关详细信息,请参阅 Wait-Job。
可靠的会话和意外断开连接
由于计算机故障或网络中断,PSSession 可能无意中断开连接。 PowerShell 尝试恢复 PSSession,但其成功取决于原因的严重性和持续时间。
意外断开连接的 PSSession 的状态可能是 “中断 ”或 “已关闭”,但也可能是 “已断开连接”。 如果 State 的值为 Disconnected,则可以使用与有意断开会话时相同的方法来管理 PSSession。 例如,可以使用 Connect-PSSession
cmdlet 重新连接到会话,使用 Receive-PSSession
cmdlet 获取在会话断开连接时运行的命令的结果。
如果关闭 (退出) 在 PSSession 中运行命令时创建 PSSession 的会话,PowerShell 会将远程计算机上的 PSSession 保持为 “已断开连接” 状态。 如果关闭 (退出) 在其中创建了 PSSession 的会话,但在 PSSession 中未运行任何命令,则 PowerShell 不会尝试维护 PSSession。