關閉一個或多個 Windows PowerShell 工作階段 (PSSession)。

語法

Remove-PSSession [[-ComputerName] <string[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

Remove-PSSession [-Id] <Int32[]> [-Confirm] [-WhatIf] [<CommonParameters>]

Remove-PSSession [-InstanceId <Guid[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

Remove-PSSession [-Name <string[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

Remove-PSSession [-Session] <PSSession[]> [-Confirm] [-WhatIf] [<CommonParameters>]

描述

Remove-PSSession Cmdlet 會關閉目前工作階段中的 Windows PowerShell 工作階段 (PSSession)。它會停止正在 PSSession 中執行的任何命令、結束 PSSession,並釋放 PSSession 所使用的資源。如果 PSSession 是連線到遠端電腦,那麼 Remove-PSSession 也會關閉本機與遠端電腦間的連線。

若要移除 PSSession,請輸入此工作階段的 Name、ComputerName、ID 或 InstanceID。

如果您已將 PSSession 儲存在變數中,則工作階段物件會保留在變數內,但是 PSSession 的狀態為 "Closed"。

參數

-ComputerName <string[]>

關閉連線至指定之電腦的 PSSession。允許使用萬用字元。

請輸入一部或多部遠端電腦的 NetBIOS 名稱、IP 位址或完整網域名稱。若要指定本機電腦,請輸入電腦名稱、"localhost" 或點 (.)。

必要?

false

位置?

1

預設值

接受管線輸入?

true (ByPropertyName)

接受萬用字元?

true

-Id <Int32[]>

關閉具有指定之識別碼的 PSSession。請輸入一個或多個識別碼 (以逗號分隔),或使用範圍運算子 (..) 指定識別碼範圍。

識別碼是整數,可唯一識別目前工作階段中的 PSSession。它比 InstanceId 更容易記得和輸入,但是只有在目前的工作階段中才具有唯一性。若要尋找 PSSession 的識別碼,請輸入 Get-PSSession 且不要指定參數。

必要?

true

位置?

1

預設值

接受管線輸入?

true (ByPropertyName)

接受萬用字元?

false

-InstanceId <Guid[]>

關閉具有指定之執行個體識別碼的 PSSession。

執行個體識別碼是一種 GUID,可唯一識別目前工作階段中的 PSSession。InstanceID 具有唯一性,即使單一電腦有多個正在執行的工作階段也一樣。

InstanceID 儲存在代表 PSSession 之物件的 InstanceID 屬性中。若要尋找目前工作階段中 PSSession 的 InstanceID,請輸入 "Get-PSSession | Format-Table Name, ComputerName, InstanceId"。

必要?

false

位置?

named

預設值

接受管線輸入?

true (ByPropertyName)

接受萬用字元?

false

-Name <string[]>

關閉具有指定之好記名稱的 PSSession。允許使用萬用字元。

因為 PSSession 的好記名稱可能會重複,所以使用 Name 參數時,請考慮一併在 Remove-PSSession 命令中使用 WhatIf 或 Confirm 參數。

必要?

false

位置?

named

預設值

接受管線輸入?

true (ByPropertyName)

接受萬用字元?

true

-Session <PSSession[]>

指定要關閉之 PSSession 的工作階段物件。請輸入包含 PSSession 的變數,或者輸入建立或取得 PSSession 的命令,例如 New-PSSessionGet-PSSession 命令。您也可以經由管道將一個或多個工作階段物件輸出至 Remove-PSSession。

必要?

true

位置?

1

預設值

接受管線輸入?

true (ByValue, ByPropertyName)

接受萬用字元?

false

-Confirm

在執行命令前先提示確認。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-WhatIf

說明執行命令時將會發生何種情況,但不會實際執行命令。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

System.Management.Automation.Runspaces.PSSession

您可以經由管道將工作階段物件輸出至 Remove-PSSession。

輸出

Remove-PSSession 不會傳回任何物件。

附註

ID 參數為必要項。使用 "remove-pssession" 時一定要指定參數。若要刪除目前工作階段中的所有 PSSession,請輸入 "Get-PSSession | remove-pssession"。

PSSession 會使用固定連線與遠端電腦連線。請建立 PSSession 來執行共用資料的一系列命令。如需詳細資訊,請參閱 about_PSSessions。

PSSession 專屬於目前的工作階段。當您結束工作階段時,您在該工作階段中建立的 PSSession 都會強制關閉。

範例 1

C:\PS>remove-pssession -id 1, 2

描述
-----------
這個命令會移除識別碼為 1 和 2 的 PSSession。






範例 2

C:\PS>get-pssession | remove-pssession 

C:\PS> remove-pssession -session (get-pssession)

C:\PS> $s = get-pssession 
C:\PS> remove-pssession -session $s".

描述
-----------
這些命令會移除目前工作階段中的所有 PSSession。雖然這三個命令格式看起來並不同,但是卻有相同的效果。






範例 3

C:\PS>$r = get-pssession -computername Serv*

$r | remove-pssession

描述
-----------
針對連線至電腦名稱開頭為 "Serv" 之電腦的所有 PSSession,這些命令會關閉這些 PSSession。






範例 4

C:\PS>get-pssession | where {$_.port -eq 90} | remove-pssession

描述
-----------
這個命令會關閉連線至連接埠 90 的 PSSession。您可以使用這個命令格式,依 ComputerName、Name、InstanceID 和 ID 以外的其他屬性來識別 PSSession。






範例 5

C:\PS>get-pssession | ft computername, instanceID  -auto

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

C:\PS> remove-pssession -InstanceID fc4e9dfa-f246-452d-9fa3-1adbdd64ae85

描述
-----------
這些命令示範如何依據 PSSession 的執行個體識別碼 (RemoteRunspaceID) 來關閉 PSSession。

第一個命令會使用 Get-PSsession Cmdlet 取得目前工作階段中的 PSSession。它會使用管線運算子 (|),將 PSSession 傳送至 Format-Table Cmdlet (別名為 ft),並由後者以表格形式格式化其 ComputerName 和 InstanceID 屬性。AutoSize 參數 ("auto") 會壓縮各欄以供顯示。

從產生的顯示中,系統管理員可以識別要關閉的 PSSession,並將該 PSSession 的 InstanceID 複製到第二個命令中。

第二個命令會使用 Remove-PSSession Cmdlet 移除具有指定之執行個體識別碼的 PSSession。






範例 6

C:\PS>function EndPSS { get-pssession | remove-pssession }

描述
-----------
此函數會刪除目前工作階段中的所有 PSSession。將此函數新增至 Windows PowerShell 設定檔之後,若要刪除所有的工作階段,只需輸入 "endpss" 即可。






請參閱




目錄