Tuesday, April 5, 2011

Log Off Terminal Server Session from a Command Prompt on based username

This script base command to clear user off a server based on username, for this to work we need to determine the session id that is required by the logoff command.

How it works :
Use the 'For' loop to search work thru the list of users on the server
Then once it has found the user then execute a logoff command

Notes:
There are 2 cases as the qwinsta doesn't display the formating the same for
disconnected sessions & active sesssions.



REM @echo off
Set servername=citrix123
Set targetname=testuser1

REM For disconnected users
for /F "tokens=1,2,3" %%i in ('qwinsta %targetname% /Server:%servername%') do if %targetname%==%%j logoff %%k /server:%servername%
REM For active users
for /F "tokens=1,2,3" %%i in ('qwinsta %targetname% /Server:%servername%') do if %targetname%==%%i logoff %%j /server:%servername%

No comments: