Logging off disconnected users

And now for something completely different… As it is essential for a DBA to know his/her way around in server administration, let’s think of something else for a minute. If your IT department consists of more than 2 people, you most likely have experienced such situation – you try to log on via RDP but there is message alike:

What happens is that standard Windows Server license allows only for 2 concurrent RDP connections to the server. If more are required, you have to install additional server role – namely Terminal Server (in 2003/2003 R2) or Remote Desktop Services (in 2008/2008 R2), but this has its consequences (primarily related to licensing).

Sometimes it’s just about sheer laziness of administrators. Instead of logging off, they just disconnect their RDP sessions still occupying a precious slot. If this is a problem, you can use command line tools available in Windows Server 2003 Admin Pack and RSAT. I’m referring here to qwinsta and rwinsta (known from Vista also as ‘query session’ and ‘reset session’):

D:\>qwinsta /?
Display information about Remote Desktop Sessions.

QUERY SESSION [sessionname | username | sessionid] 
              [/SERVER:servername] [/MODE] [/FLOW] [/CONNECT] [/COUNTER] [/VM]

sessionname         Identifies the session named sessionname. 
  username            Identifies the session with user username. 
  sessionid           Identifies the session with ID sessionid. p 
  /SERVER:servername  The server to be queried (default is current). 
  /MODE               Display current line settings. 
  /FLOW               Display current flow control settings. 
  /CONNECT            Display current connect settings. 
  /COUNTER            Display current Remote Desktop Services counters information. 
  /VM                 Display information about sessions within virtual machines
D:\>rwinsta /? 
Reset the session subsytem hardware and software to known initial values.

RESET SESSION {sessionname | sessionid} [/SERVER:servername] [/V]

sessionname         Identifies the session with name sessionname. 
  sessionid           Identifies the session with ID sessionid. 
  /SERVER:servername  The server containing the session (default is current). 
  /V                  Display additional information.

You can see my point here – you can find all sessions using qwinsta /server:servername and then for all disconnected users use rwinsta to force their log off. You can go a step further – prepare a list of servers (in a text file or somewhere in Active Directory) and run it against all of them. You can even schedule it if disconnected users are nuisance. Remember – you require administrative access to a server you’re querying.

@for /f "tokens=2" %i in ('qwinsta /server:server_name^| find /i "disc"') do @echo y | rwinsta %i /server:server_name

What happens here – you get the list of sessions from server and search for string “disc”. All lines satisfying this condition are tokenized and second token (being a session ID) is extracted. Then for each of those sessions forced log off is performed using session ID (by default, rwinsta asks for confirmation, therefore an echo).

Now if you have a list of servers prepared, just loop through it replacing server_name with a variable, or run your favourite LDAP query to get a list of servers from Active Directory. This is the final of this story, so if you have comments about it, feel free to share it.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s