Remove-PSSession

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

Syntax

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

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

若要移除 PSSession,請輸入 會話的 NameComputerNameIDInstanceID

如果您已將 PSSession 儲存在變數中,會話物件會保留在 變數中,但 PSSession 的狀態為 Closed。

範例

範例 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、NameInstanceIDID 以外的屬性來識別 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

Remove-PSSession -InstanceID fc4e9dfa-f246-452d-9fa3-1adbdd64ae85

這些命令示範如何根據其實例標識碼或 RemoteRunspaceID 關閉 PSSession

第一個命令會 Get-PSSession 使用 Cmdlet 來取得 目前會話中的 PSSession 。 它會使用管線運算符 (|) 將 PSSessions 傳送Format-Table Cmdlet,以格式化數據表中的 ComputerNameInstanceID 屬性。 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

-ContainerId

指定容器識別碼的陣列。 此 Cmdlet 會移除每個指定容器的工作階段。 docker ps使用 命令來取得容器標識碼的清單。 如需詳細資訊,請參閱 docker ps 命令的說明

Type:String[]
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Id

指定會話的識別碼陣列。 此 Cmdlet 會 關閉具有指定標識碼的 PSSession 。 輸入一或多個識別碼,並以逗號分隔,或使用範圍運算符 (..) 來指定標識符的範圍。

標識碼是整數,可唯一識別 目前會話中的 PSSession 。 比 InstanceId 更容易記住和輸入,但只在目前的會話中是唯一的。 若要尋找 PSSession 的標識碼,請執行不含參數的 Get-PSSession Cmdlet。

Type:Int32[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-InstanceId

指定實例識別碼的陣列。 此 Cmdlet 會 關閉具有指定實例識別碼的 PSSession

實例標識碼是 GUID,可唯一 識別目前會話中的 PSSession 。 實例標識碼是唯一的,即使您在單一計算機上執行多個會話也是如此。

實例標識符會儲存在代表 PSSession 之物件的 InstanceID 屬性中。 若要在目前工作階段中尋找 PSSessionsInstanceID,請輸入 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 WhatIfConfirm 參數。

Type:String[]
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

-Session

指定要關閉之 PSSession 的 工作階段物件。 輸入包含 PSSessions 的變數,或建立或取得 PSSessions 的命令,例如 New-PSSessionGet-PSSession 命令。 您也可以使用管線將一或多個工作階段物件傳送至 Remove-PSSession

Type:PSSession[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-VMId

指定虛擬機識別碼的陣列。 此 Cmdlet 會啟動每個指定虛擬機的互動式會話。 若要查看您可用的虛擬機,請使用下列命令:

Get-VM | Select-Object -Property Name, ID

Type:Guid[]
Aliases:VMGuid
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-VMName

指定虛擬機名稱的陣列。 此 Cmdlet 會啟動每個指定虛擬機的互動式會話。 若要查看您可用的虛擬機,請使用 Get-VM Cmdlet。

Type:String[]
Position:Named
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

輸入

PSSession

您可以使用管線將會話物件傳送至此 Cmdlet。

輸出

None

此 Cmdlet 不會傳回任何輸出。

備註

PowerShell 包含下列的 Remove-PSSession別名:

  • 所有平臺:

    • rsn
  • Id 參數是必要參數。 若要刪除目前工作階段中的所有 PSSession ,請輸入 Get-PSSession | Remove-PSSession

  • PSSession 會使用遠端電腦的持續性連線。 建立 PSSession 以執行一系列共享數據的命令。 如需詳細資訊,請輸入 Get-Help about_PSSessions

  • PSSession 是目前會話特有的。 當您結束會話時, 您在該工作階段中建立的 PSSession 會強制關閉。