Skip to content

Instantly share code, notes, and snippets.

@HarHarLinks
Created August 12, 2022 11:57
Show Gist options
  • Save HarHarLinks/59df3951de53dd5a876c367deceb2786 to your computer and use it in GitHub Desktop.
Save HarHarLinks/59df3951de53dd5a876c367deceb2786 to your computer and use it in GitHub Desktop.
How to trigger an action (here: PowerShell script calling a REST service) on Windows 10 upon Power On/Off/Sleep/Resume
Param([string]$cmd)
$on = '{"button": "poweron"}'
$off = '{"button": "poweroff"}'
$toggle = '{"button": "toggle_power"}'
$louder = '{"button": "vol_inc"}'
$quieter = '{"button": "vol_dec"}'
$multichannel = '{"button": "multichannel"}'
$uri = 'http://myrestservice.local:8000/button_press_post'
Switch ($cmd)
{
'on' {$cmd = $on}
'off' {$cmd = $off}
'toggle' {$cmd = $toggle}
'wake' {
Invoke-RestMethod -Method Post -ContentType application/json -Uri $uri -Body $toggle
Start-Sleep -Seconds 5
$cmd = $multichannel
}
'sleep' {
Invoke-RestMethod -Method Post -ContentType application/json -Uri $uri -Body $multichannel
$cmd = $toggle
}
'louder' {$cmd = $louder}
'quieter' {$cmd = $quieter}
Default {
'Argument not supported.'
Exit
}
}
Invoke-RestMethod -Method Post -ContentType application/json -Uri $uri -Body $cmd

Task Scheduler

In Task Scheduler, create entries for the following scenarios as needed. This example will use some arguments specific to my specific requirements (see Background) for illustration and my personal reference. Some details may need to be changed for your purpose, which I will try to point out.

If you run into trouble and need to debug things, you can activate Task History, but be aware it slows down things a lot for some reason so probably disable it again when finished.

Common setup to all:

  1. Default settings are used if not mentioned otherwise.
  2. General Tab
    • Use the following user account: (put your account here)
    • Run whether user is logged on or not
  3. Actions
    • Action: Start a program
    • Program: powershell.exe
  4. Conditions
    • Start only if the following network connection is available: Any (provided your script calls a network service as mine does)

Power On

  1. Triggers
    • Begin the task: At startup (For some reason, this is the only properly built-in preset action. Microsoft please?)
  2. Actions
    • Arguments: -File "c:\path\to\rest.ps1" on (where on is an arbitrary argument to the script)
  3. Settings
    • Run task as soon as possible after a scheduled start is missed

Power Off

  1. Triggers
    • Begin the task: On an event
    • Basic
    • Log: System
    • Source: User32
    • Event ID: 1074 (Use Event Viewer to learn more about these details, e.g.: The process C:\Windows\System32\RuntimeBroker.exe ($PC) has initiated the power off of computer $PC on behalf of user $USER for the following reason: Other (Unplanned)
  2. Actions
    • Arguments: -File "c:\path\to\rest.ps1" off

Sleep

  1. Triggers
    • Begin the task: On an event
    • Basic
    • Log: System
    • Source: Kernel-Power
    • Event ID: 187 (This is User-mode process attempted to change the system state by calling SetSuspendState or SetSystemPowerState APIs.)
  2. Actions
    • Arguments: -File "c:\path\to\rest.ps1" sleep

Resume/Wake

  1. Triggers
    • Begin the task: On an event
    • Basic
    • Log: System
    • Source: Kernel-Power
    • Event ID: 107 (This is The system has resumed from sleep.)
  2. Actions
    • Arguments: -File "c:\path\to\rest.ps1" wake
  3. Settings
    • Run task as soon as possible after a scheduled start is missed

Background

I use a surround sound system with my PC that is plugged into the Primary/Secondary power socket which turns the secondary devices on my desk on and off with the PC (primary). However the audio device's default state is not optimal (e.g. the multichannel upmix is on) which means I have to toggle it off after a cold start, which is at least every day as the power strip shutting down means it loses its standby power hence forgetting any settings. As the device is not smart on its own, but can be remote controlled through a regular infrared remote, I built an IR-RC-emulator-thingamajic that turns the thing into a RESTful smart/IoT device.

Since I dual-boot this PC, this Windows side of the scripting is matched with a systemd unit calling the respective curl commands on the Linux side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment