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

3lxsmp7m  于 2023-02-16  发布在  Shell
关注(0)|答案(2)|浏览(184)

是否有方法退出由Enter-PSSession在PowerShell中创建的每个交互式PowerShell会话?
我试过这样的方法:

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

Get-PSSession -ComputerName "my computer name" -Credential username@domain.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\username> 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\username> 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
rxztt3cl

rxztt3cl1#

尝试Disconnect-PSSession,然后是Remove-PSSession

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

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

x9ybnkn6

x9ybnkn62#

get-help Exit-PSSession -full可以看到:
Exit-PSSession [],无法通过管道将对象传输到此cmdlet。
我想你要找的是:

Get-PSSession | Disconnect-PSSession

相关问题