Remove-PSSession
關閉一或多個 PowerShell 工作階段, (PSSessions) 。
Syntax
Remove-PSSession
[-Id] <Int32[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-PSSession
[-Session] <PSSession[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-PSSession
-InstanceId <Guid[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-PSSession
-Name <String[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-PSSession
[-ComputerName] <String[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
Remove-PSSession Cmdlet 會在目前會話中關閉 PowerShell 會話 (PSSessions) 。 它會停止任何在 PSSession 中執行的命令、結束 PSSession,並釋放 PSSession 使用的資源。 若 PSSession 連線到遠端電腦,此 Cmdlet 也會關閉本機和遠端電腦之間的連線。
若要移除 PSSession,請輸入工作階段的 Name、ComputerName、ID 或 InstanceID。
若您已將 PSSession 儲存在變數中,工作階段物件仍會維持在該變數中,但 PSSession 的狀態是「已關閉」。
範例
範例 1︰使用識別碼移除工作階段
Remove-PSSession -Id 1, 2
此命令會移除具有識別碼 1 和 2 的 PSSession。
範例 2︰移除目前工作階段中的所有工作階段
Get-PSSession | Remove-PSSession
Remove-PSSession -Session (Get-PSSession)
$s = Get-PSSession
Remove-PSSession -Session $s
這些命令會移除目前工作階段中的所有 PSSession。 雖然這三個命令的格式看起來不同,但它們作用相同。
範例 3︰使用名稱關閉工作階段
$r = Get-PSSession -ComputerName Serv*
$r | Remove-PSSession
這些命令會關閉連線到名稱開頭為 Serv 之電腦的 PSSession。
範例 4︰關閉連線至連接埠的工作階段
Get-PSSession | where {$_.port -eq 90} | Remove-PSSession
此命令會關閉連線至連接埠 90 的 PSSession。 您可以使用此命令格式,以 ComputerName、Name、InstanceID 及 ID 之外的屬性來識別 PSSession。
範例 5︰根據執行個體識別碼關閉工作階段
Get-PSSession | Format-Table ComputerName, InstanceID -AutoSize
ComputerName InstanceId
------------ ----------------
Server01 875d231b-2788-4f36-9f67-2e50d63bb82a
localhost c065ffa0-02c4-406e-84a3-dacb0d677868
Server02 4699cdbc-61d5-4e0d-b916-84f82ebede1f
Server03 4e5a3245-4c63-43e4-88d0-a7798bfc2414
TX-TEST-01 fc4e9dfa-f246-452d-9fa3-1adbdd64ae85 PS C:\> Remove-PSSession -InstanceID fc4e9dfa-f246-452d-9fa3-1adbdd64ae85
這些命令示範如何根據其執行個體識別碼或 RemoteRunspaceID 來關閉 PSSession。
第一個命令會使用 Get-PSsession Cmdlet,來取得目前工作階段中的 PSSession。 它使用管線運算子 (|) 將 PSSession 傳送至 Format-Table Cmdlet,以將它們的 ComputerName 和 InstanceID 屬性格式化為表格。 AutoSize 參數會壓縮顯示的欄位。
您可以從顯示的結果中識別要關閉的 PSSession,並複製該 PSSession 的 InstanceID,然後在第二個命令中貼上。
第二個命令會使用 Remove-PSSession Cmdlet,來移除具有指定執行個體識別碼的 PSSession。
範例 6︰建立函式以刪除目前工作階段中所有的工作階段
Function EndPSS { Get-PSSession | Remove-PSSession }
此函式會刪除目前工作階段中所有的 PSSession。
將此函式新增至 PowerShell 設定檔之後,若要刪除所有工作階段,請輸入 EndPSS
。
參數
-ComputerName
指定電腦名稱的陣列。 此 Cmdlet 會關閉連線到指定電腦的 PSSession。 允許使用萬用字元。
輸入一或多部遠端電腦的 NetBIOS 名稱、IP 位址或完整網域名稱。 若要指定本機電腦,請輸入電腦名稱、localhost 或句點 (.)。
Type: | String[] |
Aliases: | Cn |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | True |
-Confirm
在執行 Cmdlet 前提示您確認。
Type: | SwitchParameter |
Aliases: | cf |
Position: | Named |
Default value: | False |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Id
指定工作階段識別碼的陣列。 此 Cmdlet 會關閉具有指定識別碼的 PSSession。 輸入一或多個識別碼 (以逗號分隔),或使用範圍運算子 (..) 來指定某個範圍的識別碼。
識別碼是整數,可唯一識別目前工作階段中的 PSSession。 與 InstanceId 相比,它比較容易記住並輸入,但是它只有在目前工作階段內是唯一的。 若要尋找 PSSession 的識別碼,請執行 Get-PSSession 且不搭配任何參數。
Type: | Int32[] |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-InstanceId
指定執行階段識別碼的陣列。 此 Cmdlet 會關閉具有指定執行個體識別碼的 PSSession。
執行個體識別碼是 GUID,可唯一識別目前工作階段中的 PSSession。 執行個體識別碼是唯一的,即使在同一部電腦上有多個正在執行的工作階段也一樣。
執行個體識別碼會儲存在代表 PSSession 之物件的 InstanceID 屬性中。
若要在目前工作階段中尋找 PSSessions 的 InstanceID,請輸入 Get-PSSession | Format-Table Name, ComputerName, InstanceId
。
Type: | Guid[] |
Position: | Named |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Name
指定工作階段好記名稱的陣列。 此 Cmdlet 會關閉具有指定好記名稱的 PSSession。 允許使用萬用字元。
由於 PSSession 的好記名稱可能不是唯一的,因此在使用 Name 參數時,請考慮在 Remove-PSSession 命令中使用 WhatIf 或 Confirm 參數。
Type: | String[] |
Position: | Named |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | True |
-Session
指定要關閉的 PSSession 工作階段物件。 輸入包含 PSSession 的變數,或輸入會建立或取得 PSSession 的命令 (例如 New-PSSession 或 Get-PSSession 命令)。 您也可以使用管線將一或多個工作階段物件傳送至 Remove-PSSession。
Type: | PSSession[] |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-WhatIf
顯示執行 Cmdlet 後會發生的情況。 Cmdlet 並不會執行。
Type: | SwitchParameter |
Aliases: | wi |
Position: | Named |
Default value: | False |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
輸入
您可以使用管線將工作階段物件傳送至此 Cmdlet。
輸出
None
此 Cmdlet 不會傳回任何物件。
備註
- Id 參數是必要的。 若要刪除目前工作階段中所有的 PSSession,請輸入
Get-PSSession | Remove-PSSession
。 - PSSession 會使用遠端電腦的持續連線。 建立 PSSession 來執行一系列共用資料的命令。 如需詳細資訊,請鍵入
Get-Help about_PSSessions
。 - PSSession 專屬於目前的工作階段。 當您結束工作階段時,即會強制關閉您在該工作階段中建立的 PSSession。