Link to home
Start Free TrialLog in
Avatar of level9tech
level9tech

asked on

Log off all users from terminal server

I am looking for the best way to log all users off of our 2003 terminal server. I need to run a scheduled task at midnight each night to run maintenance for a custom application we have that runs at 12:30 but needs all users logged off. any ideas?
Avatar of Speshalyst
Speshalyst
Flag of India image

Avatar of oBdA
oBdA

Run the script below before starting the maintenance. It will logoff any rdp session still open, excluding the one executing the command .
in its current form, you can test it safely, it will only display the logoff commands it would otherwise run.
To run it for real, simply remove the capitalized ECHO in front of logoff.exe.

@echo off
for /f "tokens="3" %%a in ('query session ^| find /i "rdp" ^| find /v ">rdp"') do (
  if %%a LSS 65536 ECHO logoff.exe %%a
)

Open in new window

Avatar of level9tech

ASKER

do I just put this script into a batch file? I am not sure how to execute it.
You can just save it as RDPLogoff.cmd or Whatever.cmd, or you can paste it into an existing batch script.
If you want to run this as an external script from another batch file, you need to start it with "call", otherwise the logoff batch won't return to the calling script.
Example:
call "C:\Scripts\RDPLogoff.cmd"

You can either execute this as a separate scheduled task 5 minutes before the maintenance task, or you can implement to maintenance task by running a batch script instead of the maintenance command directly:

@echo off
for /f "tokens="3" %%a in ('query session ^| find /i "rdp" ^| find /v ">rdp"') do (
  if %%a LSS 65536 ECHO logoff.exe %%a
)
:: *** By now, all users should be logged off.
:: *** Add the command for the maintenance task here:

Open in new window

when I run this from a command prompt for testing I am getting Session %%a not found

any ideas?
To run it directly from the command, you need to change the double percent signs to single ones:
for /f "tokens="3" %a in ('query session ^| find /i "rdp" ^| find /v ">rdp"') do (if %a LSS 65536 @ECHO logoff.exe %a)

Open in new window

I am still getting this error from the command .

C:\Documents and Settings\Administrator>for /f "tokens="3" %a in ('query session
 ^| find /i "rdp" ^| find /v ">rdp"') do (if %a LSS 65536 @ECHO logoff.exe %a)
> was unexpected at this time.

I don't understand what I am doing wrong
Sorry, that now is a typo in the original line; remove the quote between tokens= and the 3:

for /f "tokens=3" %a in ('query session ^| find /i "rdp" ^| find /v ">rdp"') do (if %a LSS 65536 @ECHO logoff.exe %a)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great! worked perfectly!! thanks for the quick response
What would I need to add to this script to log off all disconnected rdp sessions as well, not just the ones with live sessions?
Hi Axiscomputers, I use the following script in a batch file to accomplish logging out any current users and terminating disconnected sessions.  There is a one minute interval between the two processes.  I then use the task scheduler to run this task unattended.  Be certain you save the batch file in C:\Windows\System32 and then run it from there otherwise it may give you an error saying "run this script as administrator"  

@echo on
@CLS
cd\
for /f "tokens=3" %%a in ('query session ^| find /i "rdp" ^| find /v ">rdp"') do (if %%a LSS 65536 logoff.exe %%a)
ping 1.1.1.1 -n 1 -w 60000 > nul
for /f "tokens=2" %%i in ('QWinSta ^| Find /i "Disc"') Do Echo y | RWinSta %%i

Open in new window