Powershell – How to close orphaned PowerShell sessions

powershell

I was creating a script that uses several 'Invoke-Command -asjob' on a remote server, though in testing a While-Loop ran wild and I had to stop it. However, when I now try an Invoke-Command -asjob on the target server it returns the job with state failed. When I Receive the Job it returns with this error:

The WS-Management service cannot process the request. This user has exceeded the maximum number of concurrent shells allowed for this plugin. Close at least one open shell or raise the plugin quota for this user.
+ FullyQualifiedErrorId : -2144108060,PSSessionStateBroken

When I do Get-PSSession, none are listed, so I can't use Remove-PSSession (this is Google's only suggestion so far).

This is what it executed basically:

While ($True)
    { 
    Invoke-Command -Computername $RemoteServer -AsJob {Get-ChildItem}
    }

I broke out of it and did Get-Job | Remove-Job which removed all the jobs, but I still can't start an Invoke-Command -AsJob on the remote server.

I've also restarted the WSMAN Service on the remote server (by logging into it with RDP) which didn't work.

Best Answer

The remote jobs would run under a wsmprovhost.exe process per job. You should be able to brute force terminate these processes with WMI - or even remotely reboot the machine. You run the risk of killing hosted jobs for other users/activities, of course.

This would terminate all wsmprovhost.exe processes on the computer named (or array of computer names):

(gwmi win32_process -ComputerName $RemoteServer) |? 
    { $_.Name -imatch "wsmprovhost.exe" } |% 
    { $_.Name; $_.Terminate() }
Related Topic