We have a product that was using terminal services sessions to run multiple processes under multiple users. This was controlled by an overriding service.
This was working well but now and again we experienced an issue in that the service would fall over leaving some orphaned TS Sessions.
We have a batch script set to run on service failure to restart the service and send an alert message to admin staff. However this did not fix all issues, it appeared that it did not like the fact the sessions were left. We then came up with a small PowerShell script that utilises the PSTerminalServices module to log off all sessions with specific users.
Luckily the PowerShell command we used allowed for wildcards, our users were all named similarly.
- usernameProcess1
- usernameProcess2
- usernameProcess3
We could then easily get the sessions for the users above and force logoff these.
Import-Module PSTerminalServices #Get the local computername [String]$computerName = $env:computername; #Get all TS sessions with username beginning with usernameProcess* $Sessions = Get-TSSession -UserName usernameProcess* -ComputerName $computerName; ForEach ($session in $sessions) { Stop-TSSession -Computername $computerName -ID $session.sessionid -Confirm:$false -force; }
No comments:
Post a Comment