Wednesday, September 1, 2010

Log Off All Users Except (user x) from Terminal Server - Windows Batch File

Came across a situation where we needed to automatically disconnect all users from a terminal server, except for one specific user. The following script (thanks Ferdinand!) can be run as a batch file to accomplish just that; it will log off all terminal server users except for 'userx'. It gives each user a 5 minute and 1 minute warning, then runs a 'query session' to see what sessions show up and writes them to a file call 'sessions.txt'. Next it searches the session.txt file for "userx" and writes the rest of the sessions to "killts.txt", logs off those sessions/users, and deletes the .txt files that it created. It goes through the process twice to get active AND disconnected sessions.


@ECHO OFF
msg * You will be logged off in 5 minutes.
choice /T:240 /D N /N > Nul
msg * You will be disconnected in 1 minute! Please log off now!
choice /T:60 /D N /N > Nul
query session >C:\SchedTasks\sessions.txt
find /v "userx" C:\SchedTasks\sessions.txt > C:\SchedTasks\killts.txt
for /f "skip=5 tokens=3," %%i in (C:\SchedTasks\killts.txt) DO logoff %%i
query session >C:\SchedTasks\sessions.txt
find /v "userx" C:\SchedTasks\sessions.txt > C:\SchedTasks\killts.txt
for /f "skip=5 tokens=2," %%i in (C:\SchedTasks\killts.txt) DO reset session %%i
del C:\SchedTasks\sessions.txt
del C:\SchedTasks\killts.txt
EXIT


Here are some other related Windows TS/RDS commands (from thelazyadmin.com):

Query TermServer - Lists all terminal servers in the current domain.
QUERY TERMSERVER [/domain:domain] [/address][/continue]
* /domain:domain - specifies the domain (current logged on domain is default)
* /address - lists the IP address of the terminal server
* /continue - removes the pause between output screens

Query Session - Lists all current sessions running on a terminal server.
QUERY SESSION [sessionname | username | sessionid][/server:servername] [/mode] [/flow] [/connect] [/counter]
* sessionname is the name of the session that you want to query
* username is the name of the user you want to query
* sessionid is the ID of the session you want to query
* /server:servername is the name of the server you are querying
* /mode outputs the current line settings
* /flow outputs the current flow control settings /connect outputs the current connection settings
* /counter outputs the counter information for the server

Query User or Quser - Lists all current users on a terminal server
QUERY USER [username | sessionname | sessionid] [/server:servername]
* sessionname is the name of a specific session that you want to query
* username is the name of the specific user you want to query
* sessionid is the ID of the specific session you want to query
* /server:servername is the name of the server you are querying

Query Process - Lists all processes running on the terminal server.
QUERY PROCESS [ x | processid | username | sessionname | /id:nn | programname] [/server:servername] [/system]
* x lists information on all processes (note - replace x with an asterisk)
* processid lists information about only the specific process ID
* username lists processes running under the context of a specific user
* sessionname lists processes running under the context of a specific session
* /ID:nn lists processes running in the session with the specified session ID number
* programname lists all processes started by the specified executable
* /server:servername is the name of the server you are querying—the default is the server you are logged on to
* /system lists processes running under the system context

TSShutdn - Will shutdown/reboot the terminal server after a specified delay.
TSSHUTDN [wait_time] [/server:servername] [/reboot] [/powerdown] [/delay:logoffdelay] [/v]
* wait_time is the number of seconds to wait after notifying the users that the terminal server is about to shut down before forcibly logging them off (the default is 30 seconds)
* /server:servername is the name of the server to reboot/shutdown (the default is the server to which you are connected)
* /reboot reboots the server
* /powerdown powers down the server after Windows has shutdown; the servers BIOS must support this command
* /delay:logoffdelay the number of seconds to wait after logging out all users before shutting down the system (the default is 30 seconds)
* /v displays verbose information about actions being performed

Logoff - Will logoff the specified user off the terminal server and close the session. Caution, if you don't specify a user it will log you off!
LOGOFF [sessionid | sessionname] [/server:servername] [/v]
* sessionid is the ID of the session you want to logoff
* sessionname is the name of the session you want to logoff
* /server:servername specifies the name of server on which the session you want to logoff is running
* /v displays verbose information about actions being performed

Reset Session - Will kill the specified users session without warning which can be useful when a users session is stuck. Caution, if you don't specify a user it will kill your session!
RESET SESSION [sessionname | sessionid] [/server:servername] [/v]
* sessionid is the ID of the session you want to logoff
* sessionname is the name of the session you want to logoff
* /server:servername specifies the name of server on which the session you want to logoff is running
* /v displays verbose information about actions being performed

MSG - Will popup a message on the specified user(s) terminal server session.
MSG [username | sessionname | sessionid | @filename | x ][/server:servername] [/time:seconds] [/v] [/w] message
* username is the name of the user to whom you are sending the message
* sessionname is the session name to which you want to send the message
* sessionid is the ID number of the session to which you want to send the message
* @filename is the name of a text file containing usernames, sessionnames, or session IDs to which you want to send the message
* x sends the message to all users on the current or specified server (note - replace x with an asterisk)
* /server:servername specifies the server where recipients of the message are connected
* /time:seconds the number of seconds to display the message before the popup closes itself
* /v displays information about the message as it is sent
* /w causes the popup window to wait for the user to click OK before closing message is the text of the message to send

Shadow - Will allow you to shadow or take control of a users session.
SHADOW [sessionname | sessionid] [/server:servername] [/v]
* sessionid is the ID of the session you want to logoff
* sessionname is the name of the session you want to logoff
* /server:servername specifies the name of server on which the session you want to logoff is running
* /v displays verbose information about actions being performed

*** NOTE: Windows Server 2008 changed the name Terminal Services (TS) to Remote Desktop Services (RDS), but the above commands are the same. ***

13 comments:

  1. Thanks for your post and welcome to check: here.

    ReplyDelete
  2. Nice stuff you got, very interesting to read.
    Well, I do have also in my sleeves, if you have time don't forget to visit SEO Company India

    ReplyDelete
  3. I would like to thank you for the efforts you have made in writing this post. I am hoping

    the same best work from you in the future as well.

    Frases

    ReplyDelete
  4. Nice stuff you got, very interesting to read.
    Well, I do have also in my sleeves, if you have time don't forget to visit
    electrician in phoenix

    ReplyDelete
  5. Nice stuff you got, very interesting to read.
    Well, I do have also in my sleeves, if you have time don't forget to visit
    cheap voip call

    ReplyDelete
  6. I would like to thank you for the efforts you have made in writing this post. I am hoping

    the same best work from you in the future as well.

    Texas Health Insurance

    ReplyDelete
  7. I would like to thank you for the efforts you have made in writing this post. I am hoping

    the same best work from you in the future as well.

    Best Virtual Private Network Service

    ReplyDelete
  8. Nice stuff you got, very interesting to read.
    Well, I do have also in my sleeves, if you have time don't forget to visit
    Plumbing Greenville

    ReplyDelete
  9. Nice stuff you got, very interesting to read.
    Well, I do have also in my sleeves, if you have time don't forget to visit
    Greenville Plumber

    ReplyDelete
  10. I would like to thank you for the efforts you have made in writing this post. I am hoping

    the same best work from you in the future as well.

    windows virtual private server

    ReplyDelete
  11. Nice stuff you got, very interesting to read.
    Well, I do have also in my sleeves, if you have time don't forget to visit
    Missouri City Electrician

    ReplyDelete
  12. I would like to thank you for the efforts you have made in writing this post. I am hoping

    the same best work from you in the future as well.

    segunda mano

    ReplyDelete
  13. Nice stuff you got, very interesting to read.
    Well, I do have also in my sleeves, if you have time don't forget to visit
    Houston Electrician

    ReplyDelete

 
Contact our honeypot department if you are desperate to get blacklisted.