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 中的命令會繼續在遠端電腦上執行不間斷,直到命令完成或輸出緩衝區填滿為止。 若要防止完整輸出緩衝區暫停命令,請使用 、 New-PSSessionOption
或 New-PSTransportOption
Cmdlet 的 Disconnect-PSSession
OutputBufferingMode 參數。
已中斷連線的會話會維持在遠端電腦上的已中斷連線狀態。 它們可供您重新連線,直到您刪除 PSSession 為止,例如使用 Remove-PSSession
Cmdlet,或直到 PSSession 的閒置逾時到期為止。 您可以使用 、 或 New-PSTransportOption
Cmdlet 的 IdleTimeoutSec 或 IdleTimeout 參數來調整 PSSession 的Disconnect-PSSession
閒置逾New-PSSessionOption
時。
另一位使用者可以連線到您所建立的 PSSession,但前提是他們可以提供用來建立會話的認證,或使用 RunAs
會話組態的認證。
如何取得 PSSessions
從 PowerShell 3.0 開始, Get-PSSession
Cmdlet 會在本機電腦和遠端電腦上取得 PSSession。 它也可以取得在目前會話中建立的 PSSession。
若要在本機計算機或遠端電腦上取得 PSSessions,請使用 ComputerName 或 連線 ionUri 參數。 如果沒有參數, 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,而 Availability 為 None。
Get-PSSession -ComputerName Server01 | Disconnect-PSSession
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
2 Session2 Server01 Disconnected Microsoft.PowerShell None
若要建立中斷連線的會話,請使用 Cmdlet 的 Invoke-Command
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
如何連線到已中斷連線的會話
若要連線中斷連線的會話,請使用 Connect-PSSession
Cmdlet 搭配 ComputerName 或 連線 ionUri 參數。 或者,您可以將 的 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 參數來選取 [作業] 或 [主機]。 預設值為 Host。 不過,如果接收的命令是以作業的形式在目前會話中啟動,預設會以作業的形式傳回。
下列範例會 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 的狀態和可用性屬性會告訴您會話是否可供您重新連線。
當 PSSession 連接到目前的工作階段時,其狀態為 [已開啟] ,且其可用性為 [可用]。 當您中斷與 PSSession 的連線時,PSSession 狀態為 Disconnected ,其可用性為 None。
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 屬性。
使用下列命令來取得會話組態的 IdleTimeoutMs 和 MaxIdleTimeoutMs 值。
Get-PSSessionConfiguration |
Format-Table Name, IdleTimeoutMs, MaxIdleTimeoutMs
如果您是遠端電腦上的 管理員 istrators 群組成員,您可以在建立工作階段設定時設定這些值。 此外,您可以在中斷連線時變更值。
會話組態和會話選項的閑置逾時值以毫秒為單位。 會話和會話組態選項的閑置逾時值是以秒為單位。
當您建立 PSSession 時,以及當您中斷連線時,New-PSSession
Invoke-Command
您可以設定 PSSession 的閒置逾時。Disconnect-PSSession
不過,當您連接到 PSSession () 或取得結果Receive-PSSession
時Connect-PSSession
,您無法變更 IdleTimeout 值。
Connect-PSSession
和 Cmdlet 具有使用 PSSessionOption 物件的 SessionOption 參數,例如 Cmdlet 所傳回的物件New-PSSessionOption
Receive-PSSession
。
SessionOption 物件中的 IdleTimeout 值和喜好設定變數中的 IdleTimeout 值$PSSessionOption
不會在 或 Receive-PSSession
命令中Connect-PSSession
變更 IdleTimeout 的值。
若要建立具有特定閑置逾時值的 PSSession,請建立 $PSSessionOption
喜好設定變數。 將IdleTimeout屬性的值設定為所需的值(以毫秒為單位)。
當您建立 PSSessions 時,變數中的 $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 的輸出緩衝模式。
如果您是遠端電腦上的 管理員 istrators 群組成員,您可以建立和變更工作階段設定的輸出緩衝模式。
若要使用 的Drop
輸出緩衝模式建立 PSSession,請建立$PSSessionOption
喜好設定變數,其中 OutputBufferingMode 屬性的值是 Drop
。
當您建立 PSSessions 時,變數中的 $PSSessionOption
值會優先於會話組態中的值。
例如:
$PSSessionOption = New-PSSessionOption -OutputBufferingMode Drop
使用 Cmdlet 的 New-PSSessionOption
OutputBufferingMode 參數來建立具有 值的Drop
會話選項。 然後,使用 PSSessionOption 對象作為 或 Invoke-Command
Cmdlet 的 SessionOption 參數New-PSSession
值。
建立工作階段時所設定的值優先於喜好設定變數和工作階段組態中 $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。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應