Execute blocking script with admin rights in Windows before and after standby

2

I would like to run a script in Windows right before standby/sleep mode with admin rights and blocking, i.e., the system should not go to sleep until the script has successfully terminated.

Alternatively, it would be possible for me to create a background process or service if it can be done in a simple manner.

Background: It seems I have a very buggy driver for a device which leaves Windows hanging with a blank screen when going to standby or presents a blank screen upon wakeup. This is not 100% reproduceable, it happens only sometimes. However, it seems when I just disable the corresponding device in the device manager, everything works fine. I would like to automate this process.

divB

Posted 2015-08-22T17:17:00.263

Reputation: 450

Answers

2

I would like to run a script in Windows right before standby/sleep mode

You can use the Task Scheduler to create a task that is triggered by a Kernel-Power log event that indicates "Sleep":

  1. Run "Task Scheduler".
  2. Select "Task Scheduler Library
  3. Click menu "Action" > "Create Task"
  4. Select "General" and give the task a "Name" and Description"
  5. Select "Triggers" and click "New"

    • Set "Begin the Task" to "On an Event"
    • Set "Log" to "System"
    • Set "Source" to "Kernel-Power"
    • Set "Event ID" to "42" (Sleep)
    • Click "Ok"

    enter image description here

  6. Select "Actions" and click "New"

    • Set "Program/script" to a script of your choice
    • Click "Ok"

    enter image description here


When I disable the corresponding device in the device manager everything works

I would like to automate this process.

Use devcon to disable the device.

Notes:

  • Use devcon find to get the device hardware ID.
  • Use devcon disable HardwareID to disable the device.
  • Add devcon disable HardwareID to the script created above.
  • On Windows 7 64 bit you need to use the 32 bit version of devcon, as the 64 bit version gives the following error:

    This version of F:\test\devcon\ia64\devcon.exe is not compatible with the version of Windows you're running. Check your computer's system information to see whether you need a x86 (32-bit) or x64 (64- bit) version of the program, and then contact the software publisher.

  • The download link mentioned below is a zip file containing both 32 and 64 bit versions of devcon.


Where do I download devcon?

Go to the http://support.microsoft.com page The DevCon command-line utility functions as an alternative to Device Manager for the download link.


Further Reading

DavidPostill

Posted 2015-08-22T17:17:00.263

Reputation: 118 938

Thanks, great pointer but this only works for shutdown. I would need it for standby and sleep mode. – divB – 2015-08-22T18:05:58.173

1@divB Answer updated to use Task Scheduler to run a script on the appropriate Kernel-Power event (EventID 42 is Sleep). – DavidPostill – 2015-08-22T19:46:03.257

@divb Note standby and mode are the same thing. See What is the difference between Sleep and Standby modes?

– DavidPostill – 2015-08-22T19:51:34.280

This works now for standby which is great but it is not blocking: For a test, I just let execute calc.exe. When I switch to standby, calc.exe pops up potentially long after I resumed again! Since the buggy driver leaves the system hanging when it goes to standby, I need to disable it before it actually goes into standby. – divB – 2015-08-23T19:14:06.803