fortypoundhead.com

Killing Disconnected Terminal Server Sessions from the Command Line

Posted On 2013-08-26 by dwirch
Keywords:
Tags: Windows Commandline Tutorial Tip Windows Windows 8 Windows Server 2012
Views: 15367


Terminal Services (or Remote Desktop if you prefer) is a good thing. It allows you to remotely connect to a server to manage it.

However, there is a "special" breed of user out there that can't remember to log off of their sessions, opting to simply click the "X" in the upper right. Too many disconnected sessions (2), and nobody else can connect via RDP. Kill 'em quick with this process.

First, why are you using terminal services to manage your server? You should be using remote administration tools, usually via an MMC interface. All core services (AD, DHCP, DNS, etc), as well as most application services (Exchange, IIS, etc) can be administered with it. Heck, you can even use PowerShell to administer remote servers. So stop using Terminal Services to manage your servers.

Second, if you absolutely must RDP into a server, sign off correctly. Windows servers have a maximum of two RDP sessions and one console session available, unless the server has been licensed for use as a terminal server. Leaving two disconnected RDP sessions on the server effectively blocks anyone else from connecting to the server via RDP.

Normally, to clear the disconnected sessions, one would fire up the terminal services manager MMC (remote management!), connect to the server in question, and log off any disconnected sessions. It doesn't take long to do this, but can be a few minutes if you are working with a server that is on the other side of the country.

So why not try the command prompt? Windows (at least from Win7 onward, as well as server OSs) have a number of utilities built-in to manage Terminal Services remotely. The two we are interested in are:

  • Query Session
  • LogOff

The first thing you'll need to do is open a command prompt with credentials that have permissions to administer terminal services on the remote server. If you don't do this, you'll simply get "Access Denied" errors when attempting to administer the remote server.

Once you get the command prompt open, type the following command:

query session /server:MyTestServer

The output of the command will be something similar to the following:

C:\>query session /server:MyTestServer
SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
services                                    0  Disc
console                                     1  Conn
                  MyTestUser                2  Disc
rdp-tcp                                 65536  Listen
				  
C:\>


Notice the session with MyTestUser, and its current state of Disc. This means that the user did not log off the machine, but rather the connection was either disrupted, or the user simply closed the RDP window without logging off.

Just an FYI, the full syntax of the Query Session command is:

query session [{SessionName | UserName | SessionID}] [/server:ServerName] [/mode] [/flow] [/connect] [/counter]

Parameters

SessionName The name of the session you want to query.
UserName The name of the user whose sessions you want to query.
SessionID The ID of the session you want to query.
/server:ServerName Identifies the terminal server to query. The default is the current server.
/mode Displays current line settings.
/flow Displays current flow-control settings.
/connect Displays current connect settings.
/counter Displays current counters information, including the total number of sessions created, disconnected, and reconnected.
/? Displays help at the command prompt.

So, now that you have a list of the sessions, including the disconnected session, you have what you need to kick that "user" off the dead RDP connection. Make note of the ID number of the disconnected session, in this case 2.

Now, at your handy-dandy super-elevated command prompt, type the following:

Logoff 2 /server:MyTestServer

One thing you'll notice immediately is that you'll get no feedback if the command was successful. You'll simply get returned to the command prompt. You can verify the connection was disconnected by running the Query command again. The whole session would look like this:


C:\>logoff 2 /server:MyTestServer

C:\>query session /server:MyTestServer
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
 console                                     1  Conn
 rdp-tcp                                 65536  Listen
 
C:\>

Notice the previously noted dead session, which had an ID of 2, is now missing from the list. Congratulations! You have now cleared out the blocking session.

Questions? Comments? Fire off in the comments below, or even in the forums!
 


About the Author

dwirch has posted a total of 174 articles.

You can find more information from dwirch by visiting http://www.derekwirch.com.


Comments On This Post

By: dwirch
Date: 2013-08-26

Yeah, sorry about that. I clicked delete on the wrong article. My apologies for any broken links.


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.156.39.44

Before you can post, you need to prove you are human. If you log in, this test goes away.



Recent Forum Posts

New security implemented
dwirch posted on July 23, 2017 at about 6:58 in Site News

Fold Code Manager into main KB?
VB6Boy posted on July 22, 2017 at about 14:42 in Site News

Fold Code Manager into main KB?
dwirch posted on July 22, 2017 at about 14:41 in Site News

Fold Code Manager into main KB?
dwirch posted on July 21, 2017 at about 22:46 in Site News

Fold Code Manager into main KB?
dwirch posted on July 20, 2017 at about 7:55 in Site News

Job Spammer: Sam Mallon
dwirch posted on July 18, 2017 at about 18:36 in Spammers