Friday, October 31, 2008

Remotely Managing Remote Desktop

Some of my coworkers have an annoying habit of remoting into our Win2k servers and never logging out. They also like to do this in pairs, which means nobody else can remote into the machines due to Microsoft's default two-user administrative mode Terminal Services limit. Yeah, I could rclient in, or use remote MMC snap-ins, but sometimes it's just faster to manipulate the GUI via Remote Desktop.

There's a tool to remotely manage remote desktop connections in Win2k, but I couldn't find any equivalent in XP. A little searching turned up Microsoft's Windows Server 2003 Administration Tools Pack which provides server management tools that allow administrators to remotely manage Windows 2000 Servers & Windows Server 2003 family servers. And indeed it does! The kit installs the following tools, which appear under the Start, Programs, Administrative Tools menu:

Active Directory Domains and Trusts
Active Directory Management
Active Directory Sites and Services
Active Directory Users and Computers
Authorization Manager
Cluster Administrator
Connection Manager Administration Kit
DHCP
Distributed File System
DNS
IP Address Management
Network Load Balancing Manager
Public Key Management
Remote Desktops
Remote Storage
Telephony
Terminal Server Licensing
Terminal Services Manager
UDDI Services
WINS
All this for the low, low price of nothing. The only part I care about is the Terminal Services Manager, which lets me terminate idle remote desktop sessions from target servers. Take that coworkers!

You could disconnect them from a command prompt with tsdiscon:
tsdiscon 1 /server:fabrikam

Or reset their session with rwinsta:
rwinsta 1 /server:fabrikam

If you wanted to be picky/choosy about Who you disconnect/reset, you can view session info with qwinsta:
qwinsta /server:fabrikam

A couple other command line tools are tsshutdn (shutdown/reboot machine) and tskill (kill process).

in cmd do:
quser /server:

logoff /server:

query session /server:servername

find the inactive user and then:

logoff usernumber /server:servername


Windows 2000+ (includes Windows XP and 2003) have two command-line tools called qwinsta and rwinsta that can query and reset a remote session.

For example, let's say that I can't gain access to a server using Terminal Services because both sessions are used up. I can use another server to check the status of the first one. As long as the logged in user has administrative rights on the non-accessible machine I would run this:

qwinsta /server:12.12.12.12

Where 12.12.12.12 is the IP address or name of the non-accessible machine.

This will display something like this:

> qwinsta /server:12.12.12.12
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console 0 Conn wdcon
rdp-tcp 65536 Listen rdpwd
rdp-tcp#470 Bob 1 Active rdpwd
rdp-tcp#471 Jane 3 Active rdpwd

Now I know that Bob and Jane are the two that are logged in. Since Jane left the office 20 minutes ago I know that she forgot to log off. I don't know where Bob is but I only need one session so I'll ignore him for now.

To disconnect Jane's session I would type this:

rwinsta /server:12.12.12.12 3

Notice the 3 which is the session ID I found from using qwinsta above.

Yet another option with W2K3 is a new feature that lets you connect to the desktop directly and gain a 3rd session.

If you're like me, you probably noticed that rwinsta and qwinsta seems like strange names. This came from the Citrix Metaframe days which Terminal Services has descended from. It stands for:

qwinsta = Query WINdows STAtion
rwinsta = Reset WINdows STAtion

One final comment. Microsoft has replaced these two tools with Query but since qwinsta and rwinsta is fully compatible with W2K WinXP and W2K3 I choose to use it instead.


Note: In Windows Server 2003, you can right-click on the root in Terminal Services Manager and Connect to another server. Also, if you are in an Active Directory domain and using Windows 2000 Server, you can Connect to All Servers and access remote servers from the GUI. But, in a Windows 2000 Server not connected to a domain.


No comments: