0

We have a laptop that online for 24/7

in-case of power failure need to run a batch file or program

like below How to run a Windows application when the ethernet cable is connected?

3
  • I don't have a laptop at the moment but I think a Windows event is generated when it switches to battery, and you can use the "Attach a task to this event" function in the Event Viewer. Not sure which event, try experimenting.
    – LawrenceC
    Commented Aug 9, 2017 at 13:28
  • I'd try powercfg /batteryreport or Get-WMIObject -Class Win32_Battery can't test myself.
    – LotPings
    Commented Aug 9, 2017 at 13:32
  • On StackOverflow, the answer would have been RegisterPowerSettingNotification but that's assuming you have a program waiting in the background.
    – MSalters
    Commented Aug 9, 2017 at 13:54

1 Answer 1

0

This can be done via WMI

To do this, you create a WMI event customer with New-WmiEventConsumer, of -ConsumerType CommandLine so you can pass a -CommandLineTemplate.

The event itself would be a WMI filter, created with New-WmiEventFilter and -Query "select * from Win32_PowerManagementEvent where EventType = 4" (4 = suspend).

Then tie customer and event with New-WmiFilterToConsumerBinding.

Note that the problem with suspend on power failure is that you may not have much warning.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .