Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks, Eric. May I ask you, for the win, to edit and reprise the portion you think is relevant here? That'll help SFault become a choice destination for search engines, and will protect against "Kung Fu" outages or linkrot. –  pilcrow Nov 14 '09 at 3:12
    
I think that is the information you wanted. –  Eric H Nov 19 '09 at 17:49
    
Thanks eric, I have googled for hours and tried many solutions to make this work, Some of which im sure would of worked if id known about > 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. At last my disconnected clients will now log off correctly, i had issues with roaming profiles but this hows now been solved. –  user80278 May 3 '11 at 20:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.