3

Is there a way to exit every interactive PowerShell session created by Enter-PSSession in PowerShell?

I tried something like :

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

or

Get-PSSession -ComputerName "my computer name" -Credential [email protected] | foreach { Exit-PSSession $_ }

But does not seem to be right...

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.
[...]

Example of 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

and piping 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

3 Answers 3

3

Try Disconnect-PSSession followed by Remove-PSSession:

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

The Remove-PSSession cmdlet closes PowerShell sessions (PSSessions) in the current session. It stops any commands that are running in the PSSessions, ends the PSSession, and releases the resources that the PSSession was using. If the PSSession is connected to a remote computer, this cmdlet also closes the connection between the local and remote computers.

Sign up to request clarification or add additional context in comments.

6 Comments

Weirdly enough I got a Remove-PSSession : Cannot connect the PSSession because the session is not in the Disconnected state, or is not available for connection. while I do have working interactive PSSession open
Updated my answer, per the other answer try doing a disconnect first and then remove. I've not tested this but assuming that the disconnect cmdlet passes the object through.
| Disconnect-PSSession | Remove-PSSession Remove-PSSession : Cannot connect the PSSession because the session is not in the Disconnected state, or is not available for connection.
It would be worth running Get-PSSession on its own and looking at the output to see what the sessions are and what state they are in.
The issue looks to be occurring because the sessions all have an availability of "Busy", which means they are still running some command and can't be connected to. I can't see a way to work around this with the standard pssession cmdlets, but you could connect a new session to the remote machine and perhaps use it to terminate the powershell.exe processes via get-process powershell | stop-process.
|
1

From get-help Exit-PSSession -full I can see :

Exit-PSSession [] , You cannot pipe objects to this cmdlet.

What I think you are looking for is:

Get-PSSession | Disconnect-PSSession 

Comments

0

Up to Get-PSSession | Disconnect-PSSession command is ferfectly working. But Remove-PSSession is again making delay. WARNING: The network connection to computer.domain has been interrupted. Attempting to reconnect for up to 4 minutes... WARNING: Attempting to reconnect to computer.domain . WARNING: The reconnection attempt to computer.domain failed. Attempting to disconnect the session. WARNING: Computer computer.domain has been successfully disconnected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.