Pages

Tuesday, August 16, 2016

Logoff users from Remote computers


Logoff user’s active or disconnected sessions from Terminal servers
Being Windows system admins, very often we come across with situations needing to reset or logoff active or disconnected Terminal (RDP) sessions from remote Servers causing account lockouts or occupying all the allowed number of terminal server sessions.

To overcome above situations, we have to manually logoff the sessions for server(s) and to do that we either logon to the server and log off the user or do it remotely by using following commands:
Query Session /Server:ServerName
or
Query User /Server:ServerName

And then resetting the user’s session by supplying session id parameter as below:

Reset Session /Server:ServerName 5















I have written a simple batch script which does little bit of this and instead of us typing out whole command we can just put in server name and it will query remote sessions, display the output and then we need to type in session id to be reset.

Code goes below:
@echo Off
cls
echo=============================================================================
echo  This script is used to reset Terminal server Sessions from Remote server(s)
echo  causing account lockouts due to bad password or to free up Terminal Server
echo  sessions to allow new RDP connections on it.
echo.
echo  PLEASE USE WITH CAUTION, NO RESPONSIBILITY OF AUTHOR ON ANY DATA LOSS
echo  CUASED DUE TO INCORRECT/OTHER'S SESSION LOGGED OFF ACCIDENTLY:
REM Script written by Prakash Kumar (prakash82x@gmail.com) on 12:04 AM 7/25/2016
echo=============================================================================
color 0a
echo.
:Start
echo ----------------------------------------------------------------------------
Echo   Logoff active or disconnected Terminal server sessions from a server :
echo ----------------------------------------------------------------------------
echo.
set /p S=ServerName:
echo.
qwinsta /server:"%S%"
echo -----------------------------------------------------------------------------
Echo  Enter The ID to log off from above list of active/disconnected sessions:
echo -----------------------------------------------------------------------------
set /p U=SessionID:
rwinsta /server:%S% %U%
Pause
goto Start

Alternatively you can download the script from here 
Please post in the comment section below if you have any questions regarding this.
Good Luck.