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.

link|improve this question

feedback

3 Answers

up vote 3 down vote accepted

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

link|improve this answer
Thanks. Limiting to Win7 might actually be okay. – Ian C. May 1 '11 at 20:47
feedback

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
link|improve this answer
feedback

I wrote a service in Python once, to do pretty much the same thing.

link|improve this answer
Interesting. Ultimately I want to run some Python code on resume, so this may be a good approach. – Ian C. Apr 27 '11 at 16:35
@Ian: Using win32service? – grawity Apr 27 '11 at 16:48
my code? No. Straight up script that just does some log parsing and sends some messages to some daemons before quitting. – Ian C. Apr 27 '11 at 17:25
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.