Run a script when Windows resumes from suspend/hibernate state?

14

8

Is there a way to have Windows (XP, Vista and 7) run a script when a machine returns from hibernate/suspend mode? It would be okay with me if the script runs after the user unlocks a locked session after their machine resumes.

I have a service that needs to be kicked when I a machine resumes in order to get it to run properly on resume.

Ian C.

Posted 2011-04-27T15:10:10.547

Reputation: 5 383

Answers

14

In windows 7, you can do this with a scheduled task. Setup a batch script to do the actions you want and then create a task with one of the following triggers:

  • On workstation unlock - Ignores first log on, but will start after unlock.
  • On connection to user session - Every log on, can be local or remote connection.
  • On an event - In the system log, the "Power-Troubleshooter" Source will log an event code of 1 when you wake up from a sleep state.

I have not tested these to make sure that they work as expected, but I have used "On workstation lock" with high amount of success. From what I remember of scheduled tasks in XP, it only has "When I log on".

win 7 source doc

win XP source doc

Hope this helps

Doltknuckle

Posted 2011-04-27T15:10:10.547

Reputation: 5 813

The "Power-Troubleshooter" source works also with Win 8.1. – Herb – 2017-05-16T07:28:49.937

1The Power-Troubleshooter doesn't work for me in win 10 pro. The workstation unlock works anyway – e-cloud – 2018-05-03T16:54:37.883

1"On workstation unlock" works for me – sms247 – 2018-12-05T06:59:30.503

Event code 107 works for me in win 10 pro – Alex78191 – 2019-01-16T23:07:04.463

2Note that "On workstation lock/unlock" trigger options are only shown for "Create Task..." and not "Create Basic Task..." – rymo – 2012-06-14T18:37:42.333

2

Using Win32_PowerManagementEvent? I just googled it and found the following script (no warranty;).

Set oShell = CreateObject("WScript.Shell")

Set colMonitoredEvents = GetObject("winmgmts:")._
ExecNotificationQuery("Select * from Win32_PowerManagementEvent")

Do
  Set objLatestEvent = colMonitoredEvents.NextEvent

  Select Case objLatestEvent.EventType

    Case 4
      oShell.Run "Calc.exe", 1, False
      MsgBox "Entering suspend, Calc started", _
      vbInformation + vbSystemModal, "Suspend"

    Case 7
      oShell.Run "Notepad.exe", 1, False
      MsgBox "Resuming from suspend, notepad started", _
      vbInformation + vbSystemModal, "Suspend"

    Case 11
      MsgBox "OEM Event happened, OEMEventCode = " _
      & strLatestEvent.OEMEventCode

    Case 18
      MsgBox "Resume Automatic happened"

  End Select
Loop

Andy

Posted 2011-04-27T15:10:10.547

Reputation: 2 959

1

Add a scheduled task with trigger: on workstation unlock. It works, I run it after wake up from sleep. Om my Windows 2008R2 box the audio service needs to be restarted, otherwise sometimes it uses 100% of 1 cpu.

ed m

Posted 2011-04-27T15:10:10.547

Reputation: 11

-1

I actually did write such a script a while ago and blogged about it, but I do believe it was a bit after your question here. Next time I write something like this I will search Stack Overflow right away, because when people look for such things, they tend to go to this site nowadays.

regeter

Posted 2011-04-27T15:10:10.547

Reputation: 99

4While having a link is nice, could you maybe post the important parts (even the script) here, on-site? This way users won't have to go somewhere else, and in case the blog ever goes down we have a backup. – slhck – 2012-12-03T15:42:03.807