Remove-PSSession
关闭一个或多个 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)。该 cmdlet 停止在 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[]>
关闭具有指定 ID 的 PSSession。键入一个或多个 ID(以逗号分隔)或使用范围运算符 (..)可以指定 ID 的范围
ID 是一个整数,用于在当前会话中唯一标识 PSSession。它比 InstanceId 更容易记住和键入,但它仅仅在当前会话中是唯一的。要查找 PSSession 的 ID,请使用不带参数的 Get-PSSession。
是否为必需? |
true |
位置? |
1 |
默认值 |
|
是否接受管道输入? |
true (ByPropertyName) |
是否接受通配符? |
false |
-InstanceId <Guid[]>
关闭具有指定实例 ID 的 PSSession。
该实例 ID 是一个 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 的会话对象。输入包含 PSSessions 的变量或者用来创建或获取 PSSessions 的命令,如 New-PSSession 或 Get-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
说明
-----------
此命令将删除 ID 为 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。
示例 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
说明
-----------
这些命令说明如何根据实例 ID (RemoteRunspaceID) 来关闭 PSSession。
第一个命令使用 Get-PSsession cmdlet 获取当前会话中的 PSSession。该命令使用管道运算符 (|) 将这些 PSSession 发送给 Format-Table cmdlet(别名:ft),该 cmdlet 随后在表格中对它们的 ComputerName 和 InstanceID 属性进行格式化。AutoSize 参数(“auto”)用于压缩各列以便于显示。
在得到的显示结果中,管理员可以识别要关闭的 PSSession,并将该 PSSession 的 InstanceID 复制并粘贴到第二个命令中。
第二个命令使用 Remove-PSSession cmdlet 删除具有指定的实例 ID 的 PSSession。
示例 6
C:\PS>function EndPSS { get-pssession | remove-pssession }
说明
-----------
此函数删除当前会话中的所有 PSSession。在将此函数添加到 Windows Powershell 配置文件之后,要删除所有会话,只需键入“endpss”即可。
另请参阅
概念
about_PSSessions
about_Remote
New-PSSession
Get-PSSession
Enter-PSSession
Exit-PSSession
Invoke-Command