如何关闭每个交互式PowerShell会话?

时间:2019-04-30 13:09:41

标签: powershell session

是否可以退出Enter-PSSession在PowerShell中创建的每个交互式PowerShell会话?

我尝试过类似 :

Get-PSSession -ComputerName $computerName -Credential $credential | Exit-PSSession

Get-PSSession -ComputerName "pp1KYCiis01" -Credential eperret@vppp.local | foreach { Exit-PSSession $_ }

但似乎不正确...

Exit-PSSession : The input object cannot be bound to any parameters for the command either because the command does not take
pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
[...]

Get-PSSession的示例:

PS C:\Users\eperret> Get-PSSession -ComputerName $computerName -Credential $credential

 Id Name            ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            ------------    ------------    -----         -----------------     ------------
 36 WinRM7          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy
 37 WinRM4          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy
 38 WinRM2          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy
 39 WinRM5          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy
 40 WinRM3          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy

和管道Disconnect-PSSession ...

PS C:\Users\eperret> Get-PSSession -ComputerName $computerName -Credential $credential | Disconnect-PSSession

 Id Name            ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            ------------    ------------    -----         -----------------     ------------
 46 WinRM7          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy
 47 WinRM4          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy
 48 WinRM2          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy
 49 WinRM5          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy
 50 WinRM3          computerNam     RemoteMachine   Disconnected  Microsoft.PowerShell          Busy

2 个答案:

答案 0 :(得分:1)

先尝试Disconnect-PSSession,再尝试Remove-PSSession

Get-PSSession -ComputerName $computerName -Credential $credential | Disconnect-PSSession | Remove-PSSession
  

Remove-PSSession cmdlet将关闭当前会话中的PowerShell会话(PSSessions)。它停止在PSSession中运行的所有命令,结束PSSession,并释放PSSession使用的资源。如果PSSession连接到远程计算机,则此cmdlet还将关闭本地计算机和远程计算机之间的连接。

答案 1 :(得分:1)

get-help Exit-PSSession -full可以看到:

  

Exit-PSSession [],您不能将对象通过管道传递到此   cmdlet。

我认为您正在寻找的是:

Get-PSSession | Disconnect-PSSession