Search Tools Links Login

Killing Disconnected Terminal Server Sessions from the Command Line


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:

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 this post

Posted: 2013-08-26
By: dwirch
Viewed: 39,057 times

Categories

Tip

Windows Commandline

Tutorials

Windows Server

Attachments

No attachments for this post


Loading Comments ...

Comments

dwirch posted this comment on 2013-08-26:

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

You must be logged in to make a comment.

ADODB.Connection error '800a0e79'

Operation is not allowed when the object is open.

/assets/inc/inc_footer.asp, line 37