I'd like to schedule the forcible logoff of idle XP Home users, who have been "logged off" in the sense that they've been "switched" out of their console sessions, but still have processes running. How can I do this?
I'm aware of qwinsta.exe
and logoff.exe
. I think I'm close to a solution, but am not familiar enough with Windows scripting to make a coherent batch file nor scheduling (under Unix it'd be as simple as cron, awk, and xargs...).
Command Line KungFu has an excellent episode about how to boot users. I've borrowed heavily from this episode and modified it to answer pilcrow's question.
This command uses qwinsta to query a remote server for a list of sessions, and rwinsta to reset disconnected sessions:
How it works:
Qwinsta creates a table showing Terminal Sessions on the target server, serverName. For example:
Uses Qwinsta to query server serverName for all sessions, the output is piped (^|) into findstr, which uses "Disc" to match disconnected sessions. This would match only ID's 0, 20, and 76.
This FOR loop executes command1, which produces several lines of output (depending on the number of sessions). For each line of output, the 2nd and 3rd blocks of text ("tokens=2,3") are stored in %i and %j (%j is implied), then command2 is executed, which accesses %i and %j. For ID 0: %i = Disc, %j = rdpwd; for ID 20: %i = 20, %j = Disc; for ID 76: %i = 76, j% = Disc.
This displays the 2nd block of text, which could be either ID, or STATE.
Findstr matches all non-alphabetic characters, and discards the line where ID = 0 (since %i = Disc and contains alphabetic characters.
This is executed only if findstr completed successfully. Rwinsta resets the session, keyed by ID, which is now stored in %i. This would reset ID's 20 and 76.
This could be automated by putting it in a .bat file, and using Windows Scheduling to repeatedly run the job. When used in a .bat file, the For loop variable must be prefixed with an additional %, so %i would become %%i.