You might have an UPS (Uninterruptible Power Supply) at home if you are reading this article. I personally have an APC Power-Saving-Back BR550GI that I bought in Amazon, excellent value, it's tiny, quiet and perfect to protect a home computer https://www.amazon.co.uk/APC-Power-Saving-Back-UPS-PRO-Uninterruptible/dp/B002RL0CKI/ref=sr_1_1?keywords=BR550GI&qid=1561354827&s=computers&sr=1-1
Once you have installed on your machine, and with the USB cable connected to it, you can download and install the APC PowerChute Personal Edition software to manage the computer in your system, and tell it how to responds to blackouts and power cuts. Go and download the free-of-charge software from here, version 3.1.0 is only available for Windows Operating Systems: https://www.apc.com/shop/uk/en/categories/power/ups/ups-management/powerchute-personal-edition/_/N-1b6nbpp
For some mysterious reason, at the time of writing this article, APC has not included the extension on its download, therefore when you download the file, please add the .exe extension to it so that you can run it
Once you completed the installation, you should find yourself having the version 3.1.0 installed, and correctly detecting your UPS (if you have connected the USB cable). See on my example how I only have 13 minutes left of battery, not much but enough to safely shutdown my PC in case of a blackout, preventing loss of data
The problem is that PowerChute Personal Edition does not contain any means of sending you an e-mail in case of a power failure, so how do we do this? Well, let's dive into it
Get a G-mail account ready
If you don't have one already, create an account in G-mail and configure it with low security settings, so that other programs can use it to send e-mails
Click on this link to enable "less secure apps" in this on your account
And this one too, to disable the Captcha access:
Configuration of e-mails when there is a power cut
Fortunately, if you explore the Application log in your Windows computer, you will notice that PowerChute generates two messages in the event of power failures:
- Event ID 174: Battery backup transferred to battery power due to a blackout.
- Event ID 61455: Battery backup transferred to AC utility power.
What we are going to do is to use these 2 handy events to configure a script that will send us an e-mail when these events are logged, notifying us a power cut and a sub sequence power restore
Open notepad and paste on a new file the following powershell script (that needs to be save later on with the *.ps1 extension). Modify the "$to", "$Username" and "$Password" to match those of your own
Save the file as PowerOffMail.ps1 and keep in a location of your choice, then copy and paste that file into the C:\Program Files (x86)\APC directory
Before moving any further, we need to ensure that our computer will be able to run that script without problems, for that we'll set the Local Machine policy for PowerShell to "RemoteSigned". Open a privilege powershell window on your computer and then execute this command:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Once you have the LocalMachine set to "RemoteSigned", open the Computer Management in your machine > Task Scheduler > Task Scheduler Library, and create a new Basic Task called "Power Off Send Mail"
Choose the trigger "When a specific event is logged"
For the next window, choose the Application Folder > select the "APC UPS Service" and type "174" in the Event ID box, meaning that every time event 174 is trigger this task will be activated
For the next screen, type "powershell" under the field Program/Script, then on the Add Arguments (Optional) field, paste this the following string:
-file "C:\Program Files (x86)\APC\PowerOffMail.ps1"
Your configuration should look something like this:
That should be all, your Task should be set with the status of "Ready", just waiting to come into action once Event ID 174 is logged
Configuration of e-mails when the power is back
You might want to create another script and associated another task to that script, so that when Event ID 61455 (power restored) occurs, you are notified too by e-mail
This is the script I created for a file called "PowerOnMail.ps1", basically just change the $Title field to something meaningful related to a power restore
This is how the 2 x scripts should look in your "APC" folder:
And see here configured the two tasks side by side, each one of them should be pointing to their corresponding powershell script
Obviously, I cannot finish this article without telling you to test this configuration, so be brave (but just in case save all your work and close all of your windows, including the one to the garden! haha), yes be brave and pull the power cord from your UPS, wait two or three minutes and then connect it back again. Hopefully your UPS battery will have enough capacity to provide power for these 2/3 minutes of the testing, and you'll have received on your inbox the events of power off and the sub-sequence power restore.
See below my screenshot. I manually turn off my UPS for about 3 minutes, and yah, I did receive the off and restore e-mails as configured. You'll see on your computer a brief display of a powershell window when the scripts activates both ways. If you wish this can be disable this with the "-hide" switch in the arguments of the task, but I prefer to have it on, so you can verify that the script is executed. Jolly good that it all worked!
Thank you for reading! And I hope this guide helps you
London, 26 June 2019