Friday, April 16, 2010

Log off Remote Desktop Users

There are serveral DOS commands to forcibly log off the remote user. But we need to be an administrator of the target machine.

How to query for users on a machine?

First we can query to find out what users have a session on a remote machine? Windows provides the "quser" command which we can use to query for the sessions running on that machine.

The format is as follows:

quser /server:ServerName

Here is a screenshot running this command against a remote machine. Notice that it shows the User Name, state and ID of the session.


Query Users on a machine



How to log off a user of a machine?

How can we force the  user to log off. There is a handy little command called "logoff" that we can use to force a user to log off a machine based on their Session ID.

The format is as follows:

logoff SessionId /Server:ServerName


Log off User

Notice that I have used the Session Id which I got from the above.

Note: If you remotely log off a user, their log session goes away which could mean the unsaved data is lost, or if the user in the middle of an activity, we might be in trouble.


Of course some things are easier to do using a script:


Here is the VBScript code, which allows the user to remotely log off.

strComputer = ""

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems

ObjOperatingSystem.Win32Shutdown(0)

Next

Save the code in .vbs format and execute it.

No comments:

Post a Comment