Remove-PSSession
關閉一個或多個 PowerShell 會話(PSSessions)。
語法
Id (預設值)
Remove-PSSession
[-Id] <Int32[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Session
Remove-PSSession
[-Session] <PSSession[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
ContainerId
Remove-PSSession
-ContainerId <String[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
VMId
Remove-PSSession
-VMId <Guid[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
VMName
Remove-PSSession
-VMName <String[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
InstanceId
Remove-PSSession
-InstanceId <Guid[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Name
Remove-PSSession
-Name <String[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
ComputerName
Remove-PSSession
[-ComputerName] <String[]>
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
Remove-PSSession該 cmdlet 會關閉目前會話中的 PowerShell 會話(PSSessions)。 它會停止 PSSession 中正在執行的任何指令,結束 PSSession,並釋放 PSSession 正在使用的資源。 若 PSSession 連接遠端電腦,此 cmdlet 也會關閉本地與遠端電腦之間的連線。
要移除 PSSession,請輸入該會話的 名稱、 ComputerName、 ID 或 InstanceId 。
如果你已經將 PSSession 存入變數,session 物件仍保留在變數中,但 PSSession 的狀態是關閉的。
範例
範例 1:依 ID 移除會話
Remove-PSSession -Id 1, 2
此指令會移除帶有 ID 1 和 2的 PSSession 。
範例 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的電腦的 PSSessions。
範例 4:關閉連接埠的會話
Get-PSSession | where {$_.Port -eq 90} | Remove-PSSession
此指令會關閉連接至 90 埠的 PSSessions 。 您可以使用此命令格式,透過除 ComputerName、Name、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
這些指令顯示如何根據實例 ID 或 RemoteRunspaceID 關閉 PSSession。
第一個指令使用 Get-PSSession cmdlet 來取得當前會話中的 PSSessions 。
它使用管線運算子(|)將 PSSession 傳送到 Format-Table cmdlet,cmdlet 會將其 ComputerName 和 InstanceId 屬性格式化成表格。
AutoSize 參數會壓縮欄位以供顯示。
從結果顯示中,你可以辨識要關閉的 PSSession,並將該 PSSession 的 InstanceID 複製貼上到第二個指令中。
第二個指令使用 Remove-PSSession cmdlet 移除指定實例 ID 的 PSSession 。
範例 6:建立一個函式,刪除目前會話中所有的 session
function EndPSS { Get-PSSession | Remove-PSSession }
此函式會關閉當前會話中的每個 PSSession 。 在你將此函式加入 PowerShell 設定檔後,若要刪除所有會話,請輸入 EndPSS。
參數
-ComputerName
指定計算機名稱的陣列。 此指令本關閉連接至指定電腦的 PSSessions 。 允許使用通配符字元。
輸入 NetBIOS 名稱、IP 位址或一部或多部遠端電腦的完整網域名稱。 若要指定本機計算機,請輸入計算機名稱、localhost或點 (.)。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
| 別名: | Cn |
參數集
ComputerName
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Confirm
在執行 Cmdlet 之前,提示您進行確認。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 參見 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-ContainerId
指定包含容器 ID 的陣列。 此指令碼會移除每個指定容器的會話。 使用 docker ps 命令來取得容器標識碼的清單。 如需詳細資訊,請參閱 docker ps 命令的說明。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
ContainerId
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Id
指定會話的 ID 陣列。 此 cmdlet 會以指定的 ID 關閉 PSSessions 。
輸入一個或多個 ID,並以逗號分隔,或使用範圍運算子(..)來指定 ID 範圍。
ID 是一個整數,唯一識別當前會話中的 PSSession 。 它比 InstanceID 更容易記憶和輸入,但它只在當前會話中唯一。 要找到 PSSession 的 ID,請執行不帶參數的 Get-PSSession cmdlet。
參數屬性
| 類型: | Int32[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
Id
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-InstanceId
指定一組實例 ID 陣列。 此 cmdlet 會關閉具有指定實例 ID 的 PSSessions 。
實例 ID 是一個 GUID,唯一識別當前會話中的 PSSession 。 實例 ID 是唯一的,即使你在同一台電腦上有多個會話同時運行。
instance ID 會儲存在代表 PSSession 的物件的 InstanceId 屬性中。 要找到目前會話中 PSSessions 的 InstanceId,請輸入 Get-PSSession | Format-Table Name, ComputerName, InstanceId。
參數屬性
| 類型: | Guid[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
InstanceId
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Name
指定一組友善的會話名稱。 此 cmdlet 會關閉具有指定友好名稱的 PSSessions 。 允許使用通配符字元。
由於 PSSession 的友善名稱可能不唯一,使用名稱參數時,請考慮在指令中使用 Remove-PSSessionWhatIf 或 Confirm 參數。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
Name
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Session
指定 PSSessions 的會話物件要關閉。 輸入包含 PSSessions 的變數,或是建立或取得 PSSessions 的指令,例如 a New-PSSession 或 Get-PSSession 指令。 你也可以將一個或多個會話物件導管到 Remove-PSSession。
參數屬性
| 類型: | |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
Session
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-VMId
指定虛擬機的 ID 陣列。 此指令本會啟動與每個指定虛擬機的互動式會話。 要查看可用的虛擬機器,請使用以下指令:
Get-VM | Select-Object -Property Name, ID
參數屬性
| 類型: | Guid[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | VMGuid |
參數集
VMId
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-VMName
指定包含虛擬機器名稱的陣列。 此指令本會啟動與每個指定虛擬機的互動式會話。 要查看可用的虛擬機器,請使用 Get-VM cmdlet。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
VMName
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-WhatIf
顯示 Cmdlet 執行時會發生什麼事。 指令未執行。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 威斯康辛 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
PSSession
您可以透過管線將會話物件傳送至此 CMDLET。
輸出
None
此 Cmdlet 不會傳回任何輸出。
備註
PowerShell 包含下列 Remove-PSSession的別名:
所有平臺:
rsn
Id 參數是必須的。 要刪除當前會話中的所有 PSSessions ,請輸入
Get-PSSession | Remove-PSSession。PSSession 使用與遠端電腦的持久連線。 建立一個 PSSession 來執行一系列共享資料的指令。 如需詳細資訊,請鍵入
Get-Help about_PSSessions。PSSessions 是針對本學期專屬的。 當你結束一場會話時,你在該場次建立的 PSSessions 會被強制關閉。