For my server I am using an APC uninterruptible power supply for protection against blackouts and other power related issues. I built on my previous projects with Microsoft Flow to get a notification on my phone when my server loses mains power and when it regains it.
The software provided by APC with the UPS allows for basic functionality when power was lost, such as shutting down the computer before all battery power was exhausted. However, it did not provide any more advanced features, such as being able to run a script when mains power was lost or restored.
Fortunately, the software was creating application event logs when these conditions occurred. After some research and trial and error, I was able to deduce an application event with an event ID of 174 was produced when the mains power failed and an application event with an event ID of 61455 was produced when the mains became available again. (Both of these event logs came from the source of “APC UPS Service”)
Scheduling the Alert
To generate a notification in response to one of these events, I created a simple task in Windows inbuilt task scheduler. This task looked for the specified event ID and as an action run my notification PowerShell script that I created previously (see the previous blog post for details about the script).
The procedure goes as follows:
- Open task scheduler in Windows and click the link to create a basic task
- Give the new task a name and description and click the next button
- For the trigger, select the option “When a specific event is logged”
- Configure the event log for task manager to look for
- Select “Application” as the log type from the drop down menu
- In the source box enter “APC UPS Service”
- In the event ID enter either “174” for a notification on mains power failure or “61455” for a notification on mains power restoration
- Configure the action to be performed when this task runs
- Select the radio button for “Start a program”
- In the “Program/script” section, enter “powershell.exe”
- In the “Add arguments” enter the full path to the PowerShell notification script created earlier.
- Note: It is very important that you provide “powershell.exe” as the program to start. If you simply put the path to your script in this box, your PowerShell script will be opened in notepad when your task runs and not actually executed. This is just a feature of Windows and you will notice the same action if you double click on your PowerShell script.
- Optionally repeat the process for the other event ID so notification will be generated for both power failure and power restoration.