about_PSSessions

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_PSSessions

簡短描述

描述 Windows PowerShell 工作階段 (PSSession) 並說明如何建立連到遠端電腦的持續連線。

詳細描述

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

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

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

從 Windows PowerShell 3.0 開始,PSSession 與建立所在的工作階段無關。作用中 PSSession 會在遠端電腦 (或位於連線的遠端或「伺服器端」的電腦) 上維護。如此一來,您可以中斷與 PSSession 連線,稍後從同一部電腦或另一部電腦重新連接到它。

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

注意

PSSession 會使用 Windows PowerShell 遠端基礎結構。若要使用 PSSession,必須針對遠端處理設定本機和遠端電腦。如需詳細資訊,請參閱 about_Remote_Requirements。

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

什麼是工作階段?

工作階段是 Windows PowerShell 執行所在的環境。

每次您啟動 Windows PowerShell 時,系統就會為您建立一個工作階段,而您可以在工作階段中執行命令。您也可以將項目加入至您的工作階段 (例如模組和嵌入式管理單元),而且可以建立項目 (例如變數、函式和別名)。這些項目只存在於工作階段中,而且會在工作階段結束時刪除。

您也可以在本機電腦或遠端電腦上建立受使用者管理的工作階段 (也稱為「Windows PowerShell 工作階段」或 "PSSession")。如同預設工作階段,您可以在 PSSession 中執行命令以及新增和建立項目。

不過,不同於會自動啟動的工作階段,您可以控制您所建立的 PSSession。您可以取得、建立、設定和移除 PSSession,中斷其連線並重新連線,以及在相同的 PSSession 中執行多個命令。PSSession 會保持可用狀態,直到您將它刪除或逾時為止。

通常,您會建立 PSSession 以在遠端電腦上執行一系列的相關命令。當您在遠端電腦上建立 PSSession 時,Windows PowerShell 會與遠端電腦建立持續連線以支援此工作階段。

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

在 Windows PowerShell 中,「目前工作階段」是您工作所在的工作階段。「目前工作階段」可以指任何工作階段,包括暫存工作階段或 PSSession。

為何使用 PSSESSION?

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

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

當您使用 Invoke-Command 或 Enter-PSSession 的 ComputerName 參數時,Windows PowerShell 會建立與遠端電腦的暫時連線,然後在命令完成時立即關閉連線。連線關閉時,將會遺失您建立的所有資料元素。

具有 ComputerName 參數的其他 Cmdlet (例如 Get-Eventlog 和 Get-WmiObject) 會使用不同的遠端技術來收集資料。但是不會建立像 PSSession 的持續連線。

如何建立 PSSESSION

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

例如,下列命令會在 Server01 電腦上建立新的 PSSession。

        New-PSSession -ComputerName Server01

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

例如,下列命令會在 Server01 電腦上建立新的 PSSession 並在 $ps 變數中儲存結果物件。

        $ps = New-PSSession -ComputerName Server01

如何在多部電腦上建立 PSSESSION

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

例如,若要在 Server01、Server02 和 Server03 電腦上建立 PSSession,請輸入:

        New-PSSession -ComputerName Server01, Server02, Server03

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

如何取得 PSSESSION

若要取得您在目前工作階段中建立的 PSSession,請使用不含 ComputerName 參數的 Get-PSSession Cmdlet。Get-PSSession 會傳回與 New-PSSession 傳回的同類型物件。在此插入區段內文。

下列命令可取得在目前工作階段中建立的所有 PSSession。

        Get-PSSession

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

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

您也可以將 PSSession 儲存在變數中。下列命令可取得 PSSession 並將它們儲存在 $ps123 變數中。

        $ps123 = Get-PSSession

使用 PSSession Cmdlet 時,您可以依識別碼、名稱或執行個體識別碼 (GUID) 參考 PSSession。下列命令會依識別碼取得 PSSession 並將它儲存在 $ps01 變數中。

        $ps01 = Get-PSSession -Id 1

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

        Get-PSSession -ComputerName Server01

在 Windows PowerShell 2.0 中,Get-PSSession 只能取得在目前工作階段中建立的 PSSession,而不會取得在其他工作階段或其他電腦上建立的 PSSession,即使工作階段已連線到本機電腦並在其上執行命令亦然。

如何在 PSSESSION 中執行命令

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

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

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

如何刪除 PSSESSION

當您處理完 PSSession 時,請使用 Remove-PSSession Cmdlet 來刪除 PSSession 及釋出它所使用的資源。

        Remove-PSSession -Session $ps

- 或者 -

        Remove-PSSession -Id 1

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

       Remove-PSSession -ComputerName Server01 -Id 1

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

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

PSSESSION Cmdlet

    Cmdlet                Description
    -----------------     ------------------------------------------------------
    New-PSSession         Creates a new PSSession on a local or remote computer.

    Get-PSSession         Gets the PSSessions in the current session.

    Remove-PSSession      Deletes the PSSessions in the current session.

    Enter-PSSession       Starts an interactive session.

    Exit-PSSession        Ends an interactive session.
    
    Disconnect-PSSession  Disconnects a PSSession from the current session.

    Connect-PSSession     Connects a PSSession to the current session.
     
    Receive-PSSession     Gets the results of commands that ran in a disconnected
                          session.

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

       get-help *-PSSession

如需詳細資訊

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

另請參閱

about_Remote

about_Remote_Disconnected_Sessions

about_Remote_Requirements

Connect-PSSession

Disconnect-PSSession

Enter-PSSession

Exit-PSSession

Get-PSSession

Invoke-Command

New-PSSession

Remove-PSSession