In this article, I'll discuss quickly how a simple command can reset all your TS sessions on a terminal server. This is particular useful if you need to reset all terminal services sessions before you run a backup, for instance.
Prerequisites:
•The script, which can be downloaded here or at the end of this article.
•A terminal server that you have administrator access to.
For this tutorial, I'm going to step through the script to explain what happens.
Section 1: The script:
For /f "tokens=2" %i in ('QWinSta ^| Find /i "listen"') Do Echo y | RWinSta %i
The command is relatively simple, although the way I constructed it makes it appear quite complex.
First of all, the For command runs the QWinSta command, which will list all current sessions and listeners on your terminal server.
The output of the QWinSta command is then searched, using the Find command, for the word "listen".
The word "listen" designates which sessions on your terminal servers are the listeners, and consequently, if you reset a listener, all sessions running under that listener will be reset.
For instance, if all your sessions are identified as rdp-tcp#{sessionID} and you reset the rdp-tcp listener, all your rdp-tcp sessions will be reset.
Next, the For command takes the output of the Find command and looks at the second argument of each line.
The second argument is the session number of the listener.
The For command takes this session number and hands it off to the RWinSta command, which will then reset the session identified by the session number.
The Echo Y command merely says "yes" to RWinSta when RWinSta asks if you are sure you know what you are doing (and we do!).
This process will continue until all listeners identified by the Find command are reset by RWinSta.
After the RWinSta command resets the session(s), everyone will be able to log back on as soon as the system recreates new listeners.
As a side note, if you wanted to integrate this command into a script (rather than write it out on the command line), replace every instance of "%i" with "%%i" (without the quotes).
Also, if you wanted to save the output of this command to a text file, just add ">> output.txt" (without the quotes) to the end of the command.
Well, that's about it. You've just finished a walkthrough tutorial for this script.
And...no tutorial would be complete without a download, so here is the script in its entirety (compressed):
Reset_Winstations.zip
|