@ECHO OFF :: KillDisc, Version 2.21 :: Kill all disconnected NT 4 Terminal Server :: sessions, except when running relevant programs :: Written by Rob van der Woude :: http://www.robvanderwoude.com :: No command line parameters necessary for normal use of the batch file IF NOT "%1"=="" GOTO :Syntax :: For all Terminal Servers... FOR /F "TOKENS=1* DELIMS=*- " %%A IN ('QUERY TERMSERVER') DO IF "%%B"=="" CALL :FindDisc %%A GOTO :EOF :FindDisc :: SERVER = %1 ECHO. ECHO Looking for disconnected sessions on server %1 . . . FOR /F "TOKENS=1,2* DELIMS= " %%B IN ('QUERY USER /SERVER:%1 ^| FIND " disc "') DO CALL :ChkPrcs %%B %%C %1 GOTO :EOF :ChkPrcs :: USER = %1 :: ID = %2 :: SERVER = %3 ECHO. ECHO Checking relevant processes for %1 on server %3 :: ...for each disconnected session, check if there are any programs :: active, ignoring the ones specified here in the FIND /I /V commands QUERY PROCESS %1 /SERVER:%3 | FIND /I /V "explorer.exe" | FIND /I /V "winfile.exe" | FIND /I /V "icabar.exe" | FIND /I "%1" :: If no program is active, kill the session :: by logging off the user from this server IF ERRORLEVEL 1 (CALL :KillDisc %1 %2 %3) ELSE (ECHO Skipping logoff for %1 on server %3) GOTO :EOF :KillDisc :: USER = %1 :: ID = %2 :: SERVER = %3 ECHO Logging off %1 from server %3 . . . LOGOFF %2 /SERVER:%3 GOTO :EOF :Syntax CLS ECHO. ECHO KillDisc, Version 2.21 ECHO Kill all disconnected NT 4 Terminal Server sessions, ECHO except when running "relevant" programs ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com ECHO. ECHO Usage: %~n0