about_PSSessions

簡短描述

描述 PowerShell 會話 (PSSessions),並說明如何建立遠端電腦的持續性連線。

詳細描述

若要在遠端電腦上執行 PowerShell 命令,您可以使用 Cmdlet 的 ComputerName 參數,或建立 PowerShell 會話 (PSSession) 並在 PSSession 中執行命令。

當您建立 PSSession 時,PowerShell 會建立與遠端電腦的持續性連線。 使用 PSSession 在遠端電腦上執行一系列相關的命令。 在相同 PSSession 中執行的命令可以共享數據,例如變數、別名和函式的值。

您也可以在本機計算機上建立 PSSession,並在其中執行命令。 本機 PSSession 會使用PowerShell遠端基礎結構來建立和維護 PSSession。

從 Windows PowerShell 3.0 開始,Windows 上的 PSSession 與建立的會話無關。 使用中的 PSSession 會保留在遠端電腦上(或位於遠端端的電腦或連線的「伺服器端」上)。 因此,在 Windows 上,您可以從遠端 Windows 電腦上的 PSSession 中斷連線,並在稍後從同一部電腦或不同的 Windows 計算機重新連線到它。

本主題說明如何建立、使用、取得和刪除 PSSessions。 如需進階資訊,請參閱 about_PSSession_Details

注意:PSSessions 使用PowerShell遠端基礎結構。 若要使用 PSSession,必須設定本機和遠端電腦進行遠端處理。 如需詳細資訊,請參閱 about_Remote_Requirements

在 Windows Vista 和更新版本的 Windows 中,若要在本機電腦上建立 PSSession,您必須使用 [以系統管理員身分執行] 選項啟動 PowerShell。

什麼是會話?

會話是PowerShell執行所在的環境。

每次啟動PowerShell時,都會為您建立工作階段,而且您可以在工作階段中執行命令。 您也可以將專案新增至您的會話,例如模組和嵌入式管理單元,而且您可以建立專案,例如變數、函式和別名。 這些專案只存在於會話中,而且會在會話結束時刪除。

您也可以在本機計算機或遠端計算機上建立使用者管理的會話,稱為「PowerShell 會話」或「PSSessions」。 如同預設會話,您可以在 PSSession 中執行命令,並新增和建立專案。 不過,不同於自動啟動的會話,您可以控制您所建立的 PSSession。 您可以取得、建立、設定和移除它們、中斷連線並重新連線,並在相同的 PSSession 中執行多個命令。 PSSession 會維持可用狀態,直到您刪除或逾時為止。

一般而言,您會建立 PSSession,在遠端電腦上執行一系列相關的命令。 當您在遠端電腦上建立 PSSession 時,PowerShell 會建立與遠端電腦的持續連線,以支援會話。

如果您使用 Enter-PSSession Cmdlet 的 Invoke-Command ComputerName 參數來執行遠端命令或啟動互動式會話,PowerShell 會在遠端電腦上建立暫存工作階段,並在命令完成或互動式工作階段結束時立即關閉會話。 您無法控制這些暫存會話,而且不能將它們用於單一命令或單一互動式會話。

在 PowerShell 中,「目前工作階段」是您正在使用的工作階段。 「目前會話」可以參考任何會話,包括暫存會話或 PSSession。

為什麼要使用 PSSession?

當您需要遠端電腦的持續性連線時,請使用 PSSession。 透過 PSSession,您可以執行一系列共享資料的命令,例如變數的值、函式的內容或別名的定義。

您可以執行遠端命令,而不需建立 PSSession。 使用已啟用遠端 Cmdlet 的 ComputerName 參數,在一或多部電腦上執行單一命令或一系列不相關的命令。

當您使用 或Enter-PSSessionInvoke-Command ComputerName 參數時,PowerShell 會建立遠端電腦的暫時連線,然後在命令完成時立即關閉連線。 當連接關閉時,您建立的任何數據元素都將遺失。

其他具有 ComputerName 參數的 Cmdlet,例如 Get-EventlogGet-WmiObject,會使用不同的遠端技術來收集數據。 None 會建立持續性連線,例如 PSSession。

如何建立 PSSession

若要建立 PSSession,請使用 New-PSSession Cmdlet。 若要在遠端電腦上建立 PSSession,請使用 Cmdlet 的 New-PSSession ComputerName 參數。

例如,下列命令會在 Server01 計算機上建立新的 PSSession。

New-PSSession -ComputerName Server01

當您提交命令時, New-PSSession 會建立 PSSession 並傳回代表 PSSession 的物件。 您可以在建立 PSSession 時將物件儲存在變數中,也可以使用 Get-PSSession 命令在稍後取得 PSSession。

例如,下列命令會在 Server01 計算機上建立新的 PSSession,並將產生的物件儲存在$ps變數中。

$ps = New-PSSession -ComputerName Server01

如何在多部計算機上建立 PSSession

若要在多部計算機上建立 PSSessions,請使用 Cmdlet 的 New-PSSession ComputerName 參數。 在逗號分隔清單中輸入遠端電腦的名稱。

例如,若要在 Server01、Server02 和 Server03 計算機上建立 PSSession,請輸入:

New-PSSession -ComputerName Server01, Server02, Server03

New-PSSession 在每個遠端電腦上建立一個 PSSession。

如何取得 PSSessions

若要取得目前會話中建立的 PSSession,請使用Get-PSSession不含 ComputerName 參數的 Cmdlet。 Get-PSSession 會傳回傳回的相同物件 New-PSSession 類型。

下列命令會取得目前會話中建立的所有 PSSession。

Get-PSSession

PSSession 的預設顯示會顯示其標識碼和預設顯示名稱。 您可以在建立工作階段時指派替代顯示名稱。

Id   Name       ComputerName    State    ConfigurationName
---  ----       ------------    -----    ---------------------
1    Session1   Server01        Opened   Microsoft.PowerShell
2    Session2   Server02        Opened   Microsoft.PowerShell
3    Session3   Server03        Opened   Microsoft.PowerShell

您也可以將 PSSession 儲存在變數中。 下列命令會取得 PSSessions,並將其儲存在 $ps 123 變數中。

$ps123 = Get-PSSession

使用 PSSession Cmdlet 時,您可以依其標識碼、名稱或實例識別碼(GUID)來參考 PSSession。 下列命令會依標識碼取得 PSSession,並將它儲存在 $ps 01 變數中。

$ps01 = Get-PSSession -Id 1

從 Windows PowerShell 3.0 開始,PSSession 會在遠端電腦上維護。 若要取得您在特定遠端電腦上建立的 PSSessions,請使用 Cmdlet 的 Get-PSSession ComputerName 參數。 下列命令會取得您在 Server01 遠端電腦上建立的 PSSessions。 這包括在目前會話中建立的 PSSession,以及本機計算機或其他電腦上的其他會話中建立的 PSSession。

Get-PSSession -ComputerName Server01

在 Windows PowerShell 2.0 中, Get-PSSession 只會取得目前會話中建立的 PSSession。 它不會取得在其他會話或其他計算機上建立的 PSSession,即使會話已連線到本機電腦上且正在執行命令也一樣。

如何在 PSSession 中執行命令

若要在一或多個 PSSessions 中執行命令,請使用 Invoke-Command Cmdlet。 使用 Session 參數來指定 PSSessions 和 ScriptBlock 參數來指定命令。

例如,若要在儲存在 $ps 123 變數的三個 Get-ChildItem PSSession 中執行 (“dir”) 命令,請輸入:

Invoke-Command -Session $ps123 -ScriptBlock { Get-ChildItem }

如何刪除 PSSessions

當您完成 PSSession 時,請使用 Remove-PSSession Cmdlet 來刪除 PSSession,並釋放它所使用的資源。

Remove-PSSession -Session $ps

Remove-PSSession -Id 1

若要從遠端電腦移除 PSSession,請使用 Cmdlet 的 Remove-PSSession ComputerName 參數。

Remove-PSSession -ComputerName Server01 -Id 1

如果您未刪除 PSSession,則 PSSession 仍可供使用,直到逾時為止。

您也可以使用 Cmdlet 的 New-PSSessionOption IdleTimeout 參數來設定閑置 PSSession 的到期時間。 如需詳細資訊,請參閱 New-PSSessionOption

PSSession Cmdlet

如需 PSSession Cmdlet 的清單,請輸入:

Get-Help *-PSSession
  • 連線-PSSession:將 PSSession 連線 至目前的會話
  • Disconnect-PSSession:中斷與目前會話的 PSSession 連線
  • Enter-PSSession:啟動互動式會話
  • Exit-PSSession:結束互動式會話
  • Get-PSSession:取得目前會話中的 PSSession
  • New-PSSession:在本機或遠端計算機上建立新的 PSSession
  • Receive-PSSession:取得在中斷聯機會話中執行的命令結果
  • Remove-PSSession:刪除目前會話中的 PSSession

注意

只有在 Windows 上才支援已中斷連線的會話。 Connect-PSSessionDisconnect-PSSessionReceive-PSSession Cmdlet 僅適用於 Windows。 如需已中斷聯機會話的詳細資訊,請參閱 about_Remote_Disconnected_Session

詳細資訊

如需 PSSessions 的詳細資訊,請參閱 about_PSSession_Details

另請參閱