Killing open Terminal Services sessions remotely

29. February 2008 06:22 by Jaguilar in General  //  Tags:   //   Comments (0)

This tip isn't really migration-related, but I've used it so many times these past few days that I think it is worth sharing. We work with virtual machines A LOT, using Virtual Server 2005 R2 SP1. Every once in a while you need to log into a server using Terminal Services to delete old VHDs to make room for a new one, opr just for any other miscelaneous task that can't be done through the web administration client. And, unfortunately, sometimes there are two terminal services sessions open already, so you get the following error:

"Terminal server has exceeded maximum number of allowed connection"

You then stare at your monitor in frustration for a few seconds. After that, you figure out who the culprit is and ask him politely to "PLEASE LOG OFF".

Well, after discovering this tip, there is no longer a need to stare at your monitor or to ruin your relationship with your coworkers. Turns out there are a couple of command line tools you can use to close remote Terminal Services sessions. They are available in both Windows XP and 2003. The first one, qwinsta, lists all the open sessions on a particular server:

 c:\>qwinsta /server:
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 console                                     0  Conn    wdcon
 rdp-tcp                                 65536  Listen  rdpwd
                   jpena                     1  Disc    rdpwd
                   administrator             3  Disc    rdpwd

In this case, you can see that Juan Peña (user jpena) has an open session in the disconnected state. In order to close his session, you need to use the second tool, rwinsta:

rwinsta /server: 3

This command will kill the session with ID 3 (jpena's). And voilà! After this, I'm able to connect to the server. :-)

Credit goes to Scott Forsyth's WebLog.