当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 关于远程断开连接的会话

作者:精品下载站 日期:2024-12-14 02:19:17 浏览:14 分类:玩电脑

关于远程断开连接的会话


简短描述

说明如何断开与 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

  • 同一台计算机上的当前会话
  • 同一台计算机上的不同会话
  • 来自另一台计算机上的会话

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

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

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

其他用户可以连接到您创建的 PSSession,但前提是他们可以提供用于创建会话的凭据,或者使用会话配置的 RunAs 凭据。

如何获取 PSSession

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

要在本地计算机或远程计算机上获取 PSSession,请使用 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-PSSessionComputerName 参数的值为 localhost,则 Get-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 传输到 断开 PSSession

以下命令断开 PSSession 与 Server01 计算机的连接。请注意,State 属性的值为 DisconnectedAvailabilityNone

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

要创建断开连接的会话,请使用 Invoke-Command cmdlet 的 InDisconnectedSession 参数。它创建一个会话,启动命令,并在命令返回任何输出之前立即断开连接。

以下命令在远程计算机 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

如何连接到断开连接的会话

要连接断开连接的会话,请使用带有 ComputerNameConnectionUri 参数的 Connect-PSSession cmdlet。或者,您可以将 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 参数用于获取Job 中的结果。

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 的 StateAvailability 属性会告诉您该会话是否可供您重新连接。

当 PSSession 连接到当前会话时,其状态为已打开,其可用性为可用。当您与 PSSession 断开连接时,PSSession 状态为已断开,其可用性为

State 属性的值与当前会话相关。 Disconnected 值表示 PSSession 未连接到当前会话。但是,这并不意味着 PSSession 与所有会话断开连接。它可能连接到不同的会话。

要确定是否可以连接或重新连接到 PSSession,请使用可用性属性。值表示您可以连接到会话。 Busy 值表示您无法连接到 PSSession,因为它已连接到另一个会话。

以下示例在同一台计算机上的两个 PowerShell 会话中运行。请注意当 PSSession 断开连接和重新连接时,每个会话中 StateAvailability 属性值的变化。

# 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

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

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

您可以在创建 PSSession(New-PSSessionInvoke-Command)以及断开连接时(Disconnect-PSSession )。但是,当您连接到 PSSession (Connect-PSSession) 或获取结果 (Receive-PSSession) 时,您无法更改 IdleTimeout 值。

Connect-PSSessionReceive-PSSession cmdlet 有一个 SessionOption 参数,该参数采用 PSSessionOption 对象,例如由 New-PSSessionOption cmdlet 返回。

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

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

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

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

$PSSessionOption = New-PSSessionOption -IdleTimeoutMSec 172800000

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

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

例如:

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

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

例如:

Disconnect-PSSession -IdleTimeoutSec 172800

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

例如:

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

要更改会话配置的默认空闲超时和最大空闲超时,请使用 New-PSTransportOption cmdlet 的 IdleTimeoutSecMaxIdleTimeoutSec 参数。然后,使用 Set-PSSessionConfigurationTransportOption 参数值中的传输选项。

例如:

$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 属性的值为 >删除

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

例如:

$PSSessionOption = New-PSSessionOption -OutputBufferingMode Drop

使用 New-PSSessionOption cmdlet 的 OutputBufferingMode 参数创建值为 Drop 的会话选项。然后,使用 PSSessionOption 对象作为 New-PSSessionInvoke-Command cmdlet 的 SessionOption 参数的值。

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

例如:

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

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

例如:

Disconnect-PSSession -OutputBufferingMode Drop

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

例如:

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

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

例如:

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

要更改会话配置的默认输出缓冲模式,请使用 New-PSTransportOption cmdlet 的 OutputBufferingMode 参数创建一个值为 Drop。然后,使用 Set-PSSessionConfigurationSessionOption 参数值中的传输选项。

例如:

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

断开环回会话

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

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

但是,如果您使用 New-PSSessionEnter-PSSessionInvoke-CommandEnableNetworkAccess 参数cmdlet 中,环回会话是使用交互式安全令牌创建的。交互式令牌使在环回会话中运行的命令能够从其他计算机获取数据。

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

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

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

稳健的会话和意外断开连接

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

无意断开的 PSSession 的状态可能是中断关闭,但也可能是断开。如果State的值为Disconnected,您可以使用与故意断开会话时相同的技术来管理 PSSession。例如,您可以使用 Connect-PSSession cmdlet 重新连接到会话,并使用 Receive-PSSession cmdlet 获取会话断开连接时运行的命令的结果。

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

参见

  • 关于乔布斯
  • about_Remote
  • about_Remote_Variables
  • about_PSSessions
  • about_Session_Configurations
  • Invoke-Command
  • 连接-PSSession
  • 断开连接-PSSession
  • 获取 PSSession
  • 接收PS会话

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯