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-CommandInDisconnectedSession 参数会立即创建 PSSession 并断开连接。

断开连接的会话功能的工作原理

从 PowerShell 3.0 开始,PSSession 独立于在其中创建的会话。 活动 PSSession 保留在远程计算机上或服务器端连接,即使客户端计算机已关闭或断开与网络的连接。

在 PowerShell 2.0 中,当 PSSession 从原始会话断开连接或创建它的会话结束时,它会从远程计算机中被删除。

断开 PSSession 的连接时,PSSession 将保持活动状态,并保留在远程计算机上。 会话状态从运行更改为断开连接。 可以重新连接到断开连接的 PSSession:

  • 同一计算机上的当前会话
  • 同一台计算机上的不同会话
  • 从不同计算机上的会话

维护会话的远程计算机必须正在运行并连接到网络。

断开连接的 PSSession 中的命令在远程计算机上继续不间断运行,直到命令完成或输出缓冲区填充。 若要防止完整输出缓冲区挂起命令,请使用 Disconnect-PSSessionNew-PSSessionOption cmdlet 的 New-PSTransportOption 参数。

断开连接的会话在远程计算机上保持为断开状态。 它们可供你重新连接,直到删除 PSSession(例如使用 Remove-PSSession cmdlet),或者直到 PSSession 空闲超时过期为止。 可以使用 Disconnect-PSSession cmdlet 的 New-PSSessionOptionNew-PSTransportOption 参数来调整 PSSession 的空闲超时。

另一个用户可以连接您创建的 PSSessions,但前提是他们能够提供用于创建会话的凭据,或使用会话配置中的 RunAs 凭据。

如何获取 PSSessions

从 PowerShell 3.0 开始,Get-PSSession cmdlet 在本地计算机和远程计算机上检索 PSSession。 它还可以获取在当前会话中创建的 PSSession。

若要在本地计算机或远程计算机上获取 PSSessions,请使用 ComputerNameConnectionUri 参数。 如果没有参数,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

如果 Get-PSSession 参数的值是 localhost,则 Get-PSSession 获取终止并在本地计算机上维护的 PSSession。 即使在本地计算机上启动 PSSession,它也不会在 Server01 计算机上获取 PSSession。

Get-PSSession -ComputerName localhost

若要获取在当前会话中创建的会话,请使用不带参数的 Get-PSSession cmdlet。 在此示例中,Get-PSSession 获取在当前会话中创建的 PSSession,并连接到 Server01 计算机。

Get-PSSession
Id Name      ComputerName  State    ConfigurationName     Availability
-- ----      ------------  -----    -----------------     ------------
 2 Session2  Server01      Opened   Microsoft.PowerShell     Available

如何断开会话的连接

使用 Disconnect-PSSession cmdlet 断开会话的连接。 若要标识 PSSession,请使用 Session 参数,或通过管道将 PSSession 对象从 New-PSSessionGet-PSSession cmdlet 传递给 Disconnect-PSSession

以下命令将 PSSession 与 Server01 计算机断开连接。 请注意,状态属性的值为断开连接可用性的值为

Get-PSSession -ComputerName Server01 | Disconnect-PSSession
Id Name      ComputerName  State         ConfigurationName     Availability
-- ----      ------------  -----         -----------------     ------------
 2 Session2  Server01      Disconnected  Microsoft.PowerShell          None

若要创建断开连接的会话,请使用 cmdlet 的 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 与 ComputerNameConnectionUri 参数配合使用。 或者,可以通过管道将 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,而不是使用 Connect-PSSession cmdlet。 如果会话已重新连接,Receive-PSSession 获取在会话断开连接时运行的命令的结果。 如果 PSSession 仍然断开连接,Receive-PSSession 连接到它,然后获取在断开连接时运行的命令的结果。

Receive-PSSession 可以在作业(异步)或主机程序(同步)中返回结果。 使用 OutTarget 参数选择作业主机。 默认值为 主机。 但是,如果接收的命令以 作业的形式在当前会话中启动,则默认情况下,该命令将作为 作业 返回。

以下示例使用 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 属性的值是相对于当前会话的。 断开连接的值表示 PSSession 未连接到当前会话。 但是,这并不意味着 PSSession 已断开与所有会话的连接。 它可能连接到另一个会话。

若要确定是否可以连接还是重新连接到 PSSession,请使用 Availability 属性。 值为“”指示可连接会话。 忙碌值表示无法连接到 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 属性。

使用以下命令获取会话配置的 IdleTimeoutMsMaxIdleTimeoutMs 的值。

Get-PSSessionConfiguration |
  Format-Table Name, IdleTimeoutMs, MaxIdleTimeoutMs

如果你是远程计算机上的 Administrators 组的成员,可以在创建会话配置时设置这些值。 此外,可以在断开连接时更改值。

会话配置和会话选项的空闲超时值以毫秒为单位。 会话和会话配置选项的空闲超时值以秒为单位。

创建 PSSession(New-PSSessionInvoke-Command) 以及断开与 PSSession (Disconnect-PSSession) 的连接时,可以设置 PSSession 的空闲超时。 但是,连接到 PSSession()或获取结果(Connect-PSSession)时,无法更改 Receive-PSSession 值。

Connect-PSSessionReceive-PSSession cmdlet 拥有一个 SessionOption 参数,该参数接受一个 PSSessionOption 对象,例如由 New-PSSessionOption cmdlet 返回的对象。

SessionOption 对象中的 IdleTimeout 值和 首选项变量中的 $PSSessionOption 值不会更改 Connect-PSSession 命令中 Receive-PSSession 的值。

若要创建具有特定空闲超时值的 PSSession,请创建 $PSSessionOption 首选项变量。 将 IdleTimeout 属性的值设置为所需值(以毫秒为单位)。

创建 PSSessions 时,$PSSessionOption 变量中的值优先于会话配置中的值。

例如,以下命令将空闲超时设置为 48 小时:

$PSSessionOption = New-PSSessionOption -IdleTimeoutMSec 172800000

若要创建具有特定空闲超时值的 PSSession,请使用 cmdlet 的 New-PSSessionOption 参数。 然后,在 New-PSSession cmdlet 的 Invoke-Command 参数的值中使用会话选项。

创建会话时设置的值优先于 $PSSessionOption 首选项变量和会话配置中设置的值。

例如:

$o = New-PSSessionOption -IdleTimeoutMSec 172800000
New-PSSession -SessionOption $o

若要在断开连接时更改 PSSession 的空闲超时,请使用 cmdlet 的 Disconnect-PSSession 参数。

例如:

Disconnect-PSSession -IdleTimeoutSec 172800

若要创建具有特定空闲超时和最大空闲超时的会话配置,请使用 cmdlet 的 IdleTimeoutSecNew-PSTransportOption 参数。 然后,在 Register-PSSessionConfiguration 参数的值中使用传输选项。

例如:

$o = New-PSTransportOption -IdleTimeoutSec 172800 -MaxIdleTimeoutSec 259200
Register-PSSessionConfiguration -Name Test -TransportOption $o

若要更改会话配置的默认空闲超时和最大空闲超时,请使用 IdleTimeoutSec 参数。 然后,在 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 的输出缓冲模式。

如果你是远程计算机上的 Administrators 组的成员,则可以创建和更改会话配置的输出缓冲模式。

若要创建具有 Drop输出缓冲模式的 PSSession,请创建一个 $PSSessionOption 首选项变量,其中 OutputBufferingMode 属性的值为 Drop

创建 PSSessions 时,$PSSessionOption 变量中的值优先于会话配置中的值。

例如:

$PSSessionOption = New-PSSessionOption -OutputBufferingMode Drop

使用 cmdlet 的 New-PSSessionOption 参数来创建一个具有 Drop值的会话选项。 然后,使用 PSSessionOption 对象作为 New-PSSession cmdlet Invoke-Command 参数的值。

创建会话时设置的值优先于 $PSSessionOption 首选项变量和会话配置中设置的值。

例如:

$o = New-PSSessionOption -OutputBufferingMode Drop
New-PSSession -SessionOption $o

若要在断开连接时更改 PSSession 的输出缓冲模式,请使用 cmdlet 的 Disconnect-PSSession 参数。

例如:

Disconnect-PSSession -OutputBufferingMode Drop

若要在重新连接时更改 PSSession 的输出缓冲模式,请使用 cmdlet 的 New-PSSessionOption 参数。 然后,在 Connect-PSSession cmdlet 的 Receive-PSSession 参数的值中使用会话选项。

例如:

$o = New-PSSessionOption -OutputBufferingMode Drop
Connect-PSSession -ComputerName Server01 -Name Test -SessionOption $o

若要创建具有默认输出缓冲模式 Drop的会话配置,请使用 cmdlet 的 New-PSTransportOption 参数创建具有 Drop值的传输选项对象。 然后,在 Register-PSSessionConfiguration 参数的值中使用传输选项。

例如:

$o = New-PSTransportOption -OutputBufferingMode Drop
Register-PSSessionConfiguration -Name Test -TransportOption $o

若要更改会话配置的默认输出缓冲模式,请使用 cmdlet 的 New-PSTransportOption 参数创建具有 Drop值的传输选项。 然后,在 Set-PSSessionConfiguration参数的值中使用传输选项。

例如:

$o = New-PSTransportOption -OutputBufferingMode Drop
Set-PSSessionConfiguration -Name Test -TransportOption $o

断开环回会话的连接

在同一台计算机上发起和终止的会话称为环回会话或本地会话,即 PSSessions。 与其他 PSSession 一样,活动环回会话保留在连接(本地计算机)的远程端的计算机上,因此你可以断开连接并重新连接到环回会话。

默认情况下,使用不允许在会话中运行的命令访问其他计算机的网络安全令牌创建环回会话。 您可以从本地计算机或远程计算机上的任何会话重新连接到具有网络安全凭证的环回会话。

但是,如果使用 New-PSSessionEnter-PSSession cmdlet 的 Invoke-Command 参数,则会使用交互式安全令牌创建环回会话。 交互式令牌允许在环回会话中运行的命令从其他计算机获取数据。

可以使用交互式令牌断开环回会话的连接,然后从同一台计算机上的同一会话或其他会话重新连接到它们。 但是,为了防止恶意访问,只能从创建交互式令牌的计算机重新连接到环回会话。

正在等待断开连接会话中的作业

Wait-Job cmdlet 等待作业完成,然后返回到命令提示符或下一个命令。 默认情况下,如果正在运行作业的会话断开连接,则 Wait-Job 返回。 若要指示 Wait-Job cmdlet 等待会话重新连接,请在打开状态下使用强制参数。 有关详细信息,请参阅 Wait-Job

可靠的会话和无意断开连接

由于计算机故障或网络中断,PSSession 可能会意外断开连接。 PowerShell 尝试恢复 PSSession,但其成功取决于原因的严重性和持续时间。

意外断开连接的 PSSession 的状态可能会是 中断关闭,但它也可能是 断开连接。 如果状态的值为断开连接,则可以使用与有意断开会话时相同的方法来管理 PSSession。 例如,可以使用 Connect-PSSession cmdlet 重新连接到会话,使用 Receive-PSSession cmdlet 获取会话断开连接时运行的命令的结果。

如果关闭(退出)在 PSSession 中运行命令时创建 PSSession 的会话,PowerShell 会在远程计算机上的断开连接状态中维护 PSSession。 如果关闭(退出)创建 PSSession 的会话,但不在 PSSession 中运行任何命令,则 PowerShell 不会尝试维护 PSSession。

另请参阅