@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. ***
Thanks for your post and welcome to check: here.
ReplyDeleteNice stuff you got, very interesting to read.
ReplyDeleteWell, I do have also in my sleeves, if you have time don't forget to visit SEO Company India
I would like to thank you for the efforts you have made in writing this post. I am hoping
ReplyDeletethe same best work from you in the future as well.
Frases
Nice stuff you got, very interesting to read.
ReplyDeleteWell, I do have also in my sleeves, if you have time don't forget to visit
electrician in phoenix
Nice stuff you got, very interesting to read.
ReplyDeleteWell, I do have also in my sleeves, if you have time don't forget to visit
cheap voip call
I would like to thank you for the efforts you have made in writing this post. I am hoping
ReplyDeletethe same best work from you in the future as well.
Texas Health Insurance
I would like to thank you for the efforts you have made in writing this post. I am hoping
ReplyDeletethe same best work from you in the future as well.
Best Virtual Private Network Service
Nice stuff you got, very interesting to read.
ReplyDeleteWell, I do have also in my sleeves, if you have time don't forget to visit
Plumbing Greenville
Nice stuff you got, very interesting to read.
ReplyDeleteWell, I do have also in my sleeves, if you have time don't forget to visit
Greenville Plumber
I would like to thank you for the efforts you have made in writing this post. I am hoping
ReplyDeletethe same best work from you in the future as well.
windows virtual private server
Nice stuff you got, very interesting to read.
ReplyDeleteWell, I do have also in my sleeves, if you have time don't forget to visit
Missouri City Electrician
I would like to thank you for the efforts you have made in writing this post. I am hoping
ReplyDeletethe same best work from you in the future as well.
segunda mano
Nice stuff you got, very interesting to read.
ReplyDeleteWell, I do have also in my sleeves, if you have time don't forget to visit
Houston Electrician