logoff user & go to sleep

Home Forums Scripting General Scripting logoff user & go to sleep

This topic contains 6 replies, has 4 voices, and was last updated by Avatar gemmiti 5 years, 11 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • Avatar
    APOC
    Member
    #163493

    is there a way to script a user log off and then go to sleep (delayed?)
    I’d like a one-click solution to log off the current user (windows 7 pro) and that the computer will go to sleep.

    ie. a delayed shutdown.exe with the LogOff command won’t work because the /l (or -l) switch can’t be used with the /t (or -t) switch for delay.

    REM Log OFF
    %windir%system32shutdown.exe -l -f
    REM Go to Sleep
    %windir%System32rundll32.exe powrprof.dll,SetSuspendState 0,1,0

    This won’t work in a script because the system will fall asleep first, if you wake-up the system again THEN it’ll Log Off.

    Rems
    Rems
    Moderator
    #228424

    Re: logoff user & go to sleep

    APOC;282733 wrote:
    is there a way to script a user log off and then go to sleep (delayed?)

    Nope you can’t accomplish this with just one script. The command shutdown.exe -l -f will logoff the user. When the user is logged off the running script (executed by the user) is killed = before it is time you want the ‘go to sleep’ command to run.

    Maybe you can try a scheduled task for bringing the computer to sleep. A task that is triggerd to run every time a user has logged off.
    (trigger: On an Event / Log is: System / Source is: Winlogon / event ID is: 7002 / delay task for: 30 seconds. General: run the task with the following account: / Run whether user is logged on or not)
    When triggered it starts a batch.
    i.e.;

    Code:
    @echo off

    >NUL 2>&1 call:chkforproc wuaudt.exe

    start %windir%System32RUNDLL32.EXE powrprof.dll,SetSuspendState 0,1,0

    GOTO:EOF

    :chkforproc
    Set /a n+=1
    timeout.exe /t 1 /nobreak
    If %n% gtr 5 ([COLOR=”Red”][B]EXIT[/B][/COLOR])
    (Tasklist.exe /FI “IMAGENAME eq %*” /NH | findstr.exe /N /IC:”%*”)&&(
    timeout.exe /t 4 /nobreak
    goto :chkforproc
    )
    exit /b 0

    You still have to test it if it works well in all circumstances. I would recommend to use the delays applied in the task and the script.

    /Rems

    Avatar
    igor7
    Member
    #295080

    Re: logoff user & go to sleep

    Quote:
    You still have to test it if it works well in all circumstances

    Thanks, Rems!!
    This solution working perfectly on Windows 7 Ent/Pro. I’m also searched for something similar…
    Just one remark… If in the system power plan enabled hibernation, the command SetSuspendState 0,1,0
    will cause computer hibernate instead of sleep. In order to force it sleep after log off, hibernation should be turned off. Reference:
    http://msdn.microsoft.com/en-us/library/aa373201.aspx
    Alternatively it is possible to use NirCmd.exe tool for forcing computer to go into standby mode even when hibernation is enabled:
    http://www.nirsoft.net/utils/nircmd.html

    Avatar
    gemmiti
    Member
    #383505

    Re: logoff user & go to sleep

    ( I already disabled Hibernate, you can do this easily with: powercfg -h off )

    @rems:
    Great Workaround, still a slight problem for my situation though
    Now the system always goes to sleep when a user log’s off, this is almost what I want and need.

    Situation:
    I use a big-screen for Narrowcasting, when the system is ‘logged off’ a default screen saver shows all our important projects (also when a user is ‘logged on’ the same screen saver is enabled by default).
    SO I don’t always want the computer set to standby/sleep when a user does log-off (the system is also being used for presentations by multiple users).
    BUT sometimes a user specifically wants it to go to sleep when there are people in the room and the big-screen is only distracting, I know one can just turn off the screen, but putting the whole system a sleep is energy wise a better/smarter solution imo.

    It’s really needed on demand somehow, hoping to find some way to make this work. I’ll keep searching for a possible working combination with your directions, really appreciate your example for a possible direction.
    PS. Some thoughts: maybe call/run/start a (delayed) schedule for sleep from batch in combination with log-off

    I hope I sketched out the situation clear enough?

    Rems
    Rems
    Moderator
    #228425

    Re: logoff user & go to sleep

    Maybe something like,
    The logoff batch adds some folder or other entry (user must have write permissions) with an unusual name before it logs off the user.

    The batch lauched by the task scheduler check if the folder exist. If it exist it deletes the folder then do the rest of the commands and the computer go to sleep. If the folder didn’t exist the batch does not continue.

    /Rems

    Rems
    Rems
    Moderator
    #228426

    Re: logoff user & go to sleep

    igor7;283024 wrote:
    Thanks, Rems!!
    This solution working perfectly on Windows 7 Ent/Pro. I’m also searched for something similar…
    Just one remark… If in the system power plan enabled hibernation, the command SetSuspendState 0,1,0
    will cause computer hibernate instead of sleep. In order to force it sleep after log off, hibernation should be turned off. Reference:
    http://msdn.microsoft.com/en-us/library/aa373201.aspx
    Alternatively it is possible to use NirCmd.exe tool for forcing computer to go into standby mode even when hibernation is enabled:
    http://www.nirsoft.net/utils/nircmd.html

    Thank you igor7 for the additional information.

    /Rems

    Rems
    Rems
    Moderator
    #228427

    Re: logoff user & go to sleep

    If have modified the “:chkforproc” subroutine a bit, Goto:EOF is replaced by EXIT

    The goto:EOF command ends the subroutine and return back to the point from where the subroutine was called. That is not desired because the script should be ended at that point.

    The reason why I added the subroutine is because when a user shutdown the computer, s/he will be logged of first (this triggers the task) before the computer shutdown. And when the shutdown takes longer than usual, i.e. when it is installing updates, the computer goes to sleep while wuaudt.exe is running.

    /Rems

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.