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.
- Default settings are used if not mentioned otherwise.
- General Tab
- Use the following user account: (put your account here)
- Run whether user is logged on or not
- Actions
- Action: Start a program
- Program:
powershell.exe
- Conditions
- Start only if the following network connection is available: Any (provided your script calls a network service as mine does)
- Triggers
- Begin the task: At startup (For some reason, this is the only properly built-in preset action. Microsoft please?)
- Actions
- Arguments:
-File "c:\path\to\rest.ps1" on
(whereon
is an arbitrary argument to the script)
- Arguments:
- Settings
- Run task as soon as possible after a scheduled start is missed
- 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)
- Actions
- Arguments:
-File "c:\path\to\rest.ps1" off
- Arguments:
- Triggers
- Begin the task: On an event
- Basic
- Log: System
- Source:
Kernel-Power
- Event ID:
187
(This isUser-mode process attempted to change the system state by calling SetSuspendState or SetSystemPowerState APIs.
)
- Actions
- Arguments:
-File "c:\path\to\rest.ps1" sleep
- Arguments:
- Triggers
- Begin the task: On an event
- Basic
- Log: System
- Source:
Kernel-Power
- Event ID:
107
(This isThe system has resumed from sleep.
)
- Actions
- Arguments:
-File "c:\path\to\rest.ps1" wake
- Arguments:
- Settings
- Run task as soon as possible after a scheduled start is missed
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.