Remove-PSSession
關閉一個或多個 PowerShell 工作階段(PSSessions)。
語法
Remove-PSSession
[-Id] <Int32[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-PSSession
[-Session] <PSSession[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-PSSession
-ContainerId <String[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-PSSession
-VMId <Guid[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-PSSession
-VMName <String[]>
[-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)。 它會停止在 PSSessions中執行的任何命令、結束 PSSession,並釋放使用 PSSession 的資源。 如果 PSSession 連線到遠端電腦,此 Cmdlet 也會關閉本機和遠端電腦之間的連線。
若要移除 PSSession,請輸入會話的 Name、ComputerName、ID或 InstanceID。
如果您已將 PSSession 儲存在變數中,會話物件會保留在變數中,但 PSSession 的狀態為 Closed。
範例
範例 1:依識別碼移除會話
Remove-PSSession -Id 1, 2
此命令會移除具有標識碼 和 1
的 2
。
範例 2:移除目前會話中的所有會話
Get-PSSession | Remove-PSSession
Remove-PSSession -Session (Get-PSSession)
$s = Get-PSSession
Remove-PSSession -Session $s
這些命令會移除目前會話中所有 PSSessions。 雖然這三種命令格式看起來不同,但它們具有相同的效果。
範例 3:依名稱關閉工作階段
$r = Get-PSSession -ComputerName Serv*
$r | Remove-PSSession
這些命令會關閉名稱開頭為 的電腦所連線的 Serv
。
範例 4:關閉連線到埠的會話
Get-PSSession | Where-Object {$_.port -eq 90} | Remove-PSSession
此命令會關閉連線至埠 90 的 PSSessions。 您可以使用此命令格式藉由除了 ComputerName、名稱、InstanceID和 ID之外的屬性來識別 PSSessions。
範例 5:依實例 ID 關閉工作階段
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
Remove-PSSession -InstanceID fc4e9dfa-f246-452d-9fa3-1adbdd64ae85
這些命令示範如何根據其實例標識碼,或 RemoteRunspaceID關閉 PSSession。
第一個命令會使用 Get-PSSession
cmdlet 來取得目前會話中的 PSSessions。
它會使用管線運算符 (|
) 將 PSSessions 傳送至 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 會關閉連接到指定電腦的 PSSessions。 允許使用通配符字符。
輸入 NetBIOS 名稱、IP 位址或一部或多部遠端電腦的完整網域名稱。 若要指定本機計算機,請輸入計算機名稱、localhost
或點 (.
)。
類型: | String[] |
別名: | Cn |
Position: | 0 |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | True |
-Confirm
在執行 Cmdlet 之前,提示您進行確認。
類型: | SwitchParameter |
別名: | cf |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-ContainerId
指定包含容器 ID 的陣列。 此 Cmdlet 會移除每個指定容器的會話。 使用 docker ps
命令來取得容器標識碼的清單。 如需詳細資訊,請參閱 docker ps 命令的說明。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-Id
指定會話的識別碼陣列。 此命令將會關閉具有指定標識碼的 PSSessions。
輸入一或多個識別碼,並以逗號分隔,或使用範圍運算符 (..
) 來指定標識符的範圍。
在目前會話中,ID 是一個整數,用於唯一識別 PSSession。 比 InstanceId更容易記住和輸入,但只在當前的會話中是唯一的。 若要尋找 PSSession的標識碼,請執行不含參數的 Get-PSSession
Cmdlet。
類型: | Int32[] |
Position: | 0 |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-InstanceId
指定一組實例識別碼。 此 Cmdlet 會關閉具有指定實例識別碼的 PSSessions。
實例標識碼是 GUID,用於唯一識別目前會話中的 PSSession。 實例標識碼是唯一的,即使您在單一計算機上執行多個會話也是如此。
實例標識碼會儲存在 物件的 InstanceID 屬性中,代表 PSSession。 若要在目前工作階段中尋找 PSSessions 的 InstanceID,請輸入 Get-PSSession | Format-Table Name, ComputerName, InstanceId
。
類型: | Guid[] |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-Name
指定工作階段易記名稱的陣列。 此 Cmdlet 會關閉具有指定易記名稱的 PSSessions。 允許使用通配符字符。
因為 PSSession 的易記名稱可能不是唯一的,因此當您使用 Name 參數時,也請考慮在 命令中使用 WhatIf 或 Remove-PSSession
參數。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | True |
-Session
指定要關閉之 PSSessions 的工作階段物件。 輸入包含 PSSessions 的變數,或是用來建立或取得 PSSessions的命令,例如 New-PSSession
或 Get-PSSession
命令。 您也可以將一個或多個工作階段物件透過管道傳送至 Remove-PSSession
。
類型: | PSSession[] |
Position: | 0 |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-VMId
指定一個虛擬機器識別碼的陣列。 此 Cmdlet 會啟動每個指定虛擬機的互動式會話。 若要查看您可用的虛擬機,請使用下列命令:
Get-VM | Select-Object -Property Name, ID
類型: | Guid[] |
別名: | VMGuid |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-VMName
指定包含虛擬機器名稱的陣列。 此 Cmdlet 會啟動每個指定虛擬機的互動式會話。 若要查看可用的虛擬機,請使用 Get-VM
Cmdlet。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-WhatIf
顯示 Cmdlet 執行時會發生什麼事。 Cmdlet 並未執行。
類型: | SwitchParameter |
別名: | wi |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
輸入
您可以透過管線將會話物件傳送至此 CMDLET。
輸出
None
此 Cmdlet 不會傳回任何輸出。
備註
PowerShell 包含下列 Remove-PSSession
別名:
所有平臺:
rsn
識別碼 參數是必要的。 若要刪除目前會話中的所有 PSSessions,請輸入
Get-PSSession | Remove-PSSession
。PSSession 會使用遠端電腦的持續性連線。 建立 PSSession,以執行一系列共享數據的命令。 如需詳細資訊,請輸入
Get-Help about_PSSessions
。PSSessions 是目前會話特有的。 當您結束會話時,您在該會話中建立的 PSSessions 會被強制關閉。