End RemoteDesktop Session and Standby
create a batch file (or really something) which i can activate from my
remote-pc to end the session and put the pc to sleep (does not have to
log out the user)
Two example scripts below:
- with a
LOGOFF
; and,
- another with an RDP
DISCONNECT
(no logoff)
I wasn't sure if you wanted one or the other so I put both below which should work fine from Windows 7 from what I tested.
For the logoff script, I put some if logic to not logoff console, system, or rdp-tpc listening sessions since you're only interesting in RDP connected remote sessions.
For the disconnect script you will just disconnect the session that runs it as needed so no need for additional logic on this one (this is the one you probably need).
The assumption is you will run this as a saved batch script manually and if you decide to use the logoff script rather than the disconnect script, you will have permission to logoff all or any applicable sessions.
BATCH SCRIPTS
(This script will also wait 5 seconds after the disconnect of the current session and then put the PC to sleep.)
Batch Script Example (DISCONNECT
all sessions)
@ECHO OFF
::: Disconnected current RDP session
TSDISCON
::: Pause 5seconds before going to sleep ensuring enough time
PING -n 5 127.0.0.1 > NUL
::: Put computer to sleep
%systemroot%\System32\rundll32.exe powrprof.dll,SetSuspendState 0,1,0
GOTO EOF
(This script will also wait 30 seconds after the logoff all the sessions and then put the PC to sleep.)
WARNING: This script will not be able to be run ad hoc by double clicking this batch file for the OP's needs specifically, but I kept this here as a bonus item in case anyone finds use with it otherwise. Since it will logoff the accounts before the sleep logic runs, that sleep logic will not work in that particular case. This would need to be run from Task Scheduler, with Highest Privileges
, and Run whether the user is logged on or not
—I supposed you could setup this way and NOT on a schedule and just kick off/run as needed from Task Scheduler too. (screen shot below)
Batch Script Example (LOGOFF
all sessions)
@ECHO OFF
FOR /F %%S IN (
'"QUERY SESSION /SM"'
) DO (
IF NOT %%~S==0 IF NOT %%~S==1 IF NOT %%~S==65536 RWINSTA %%~S
)
::: Pause 5 seconds before going to sleep ensuring enough time
PING -n 5 127.0.0.1 > NUL
::: Put computer to sleep
%systemroot%\System32\rundll32.exe powrprof.dll,SetSuspendState 0,1,0
GOTO EOF
Screen Shot Scheduled Task Options
Further Research, Reading, and Sources