Windows – Schedule hard logoff of idle users

scheduled-taskwindowswindows-xp

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…).

Best Answer

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:

FOR /F "tokens=2,3" %i IN ('qwinsta /server:serverName ^| findstr "Disc"') DO @echo %i | findstr /v "[a-z]" && rwinsta /server:serverName %i

How it works:

Qwinsta creates a table showing Terminal Sessions on the target server, serverName. For example:

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
                                             0  Disc    rdpwd              
 rdp-tcp                                 65536  Listen  rdpwd              
 console                                    15  Conn    wdcon              
 rdp-tcp#64        joeuser                   8  Active  rdpwd              
 rdp-tcp#84        janeuser                 17  Active  rdpwd              
                   janetuser                20  Disc    rdpwd              
 rdp-tcp#300       jimuser                  25  Active  rdpwd              
                   processid                76  Disc    rdpwd               

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.

'qwinsta /server:serverName ^| findstr "Disc"'

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.

FOR /F "tokens=2,3" %i IN (command1) DO command2

This displays the 2nd block of text, which could be either ID, or STATE.

@echo %i

Findstr matches all non-alphabetic characters, and discards the line where ID = 0 (since %i = Disc and contains alphabetic characters.

findstr /v "[a-z]"

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.

rwinsta /server:serverName %i

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.