Recently, I setup a server which runs a few VMs in my homelab. Physically attached to this server is an APC BackUPS 1300 UPS unit. In case of a power outage, my goal was to have one of the Windows VM guests trigger a shutdown to the host, while also safely shutting down each VM in sequence.

I found a great resource which described how to setup apcupsd that resides on the Windows VM and monitors the APC UPS that is passed through as a USB device.

After getting everything working via testing the doshutdown.bat by running it manually, the host did in fact receive the proper commands via plink, and shutdown everything properly (first all VMs, then the host itself).

The problem was that when apcupsd hit one of the thresholds which was to trigger a shutdown, it was calling my new doshutdown.bat script, but nothing was happening. I figured it was maybe due to some kind of permissions issue as the script was being called by the system, but I wasn’t sure.

Finally, I found my answer. Simon Rozman describes that certain registry keys must exist in order for plink to connect to the remote host as the SYSTEM account:

HKEY_USERS\S-1-5-18\Software\SimonTatham\PuTTY\SshHostKeys
HKEY_USERS\S-1-5-19\Software\SimonTatham\PuTTY\SshHostKeys
HKEY_USERS\S-1-5-20\Software\SimonTatham\PuTTY\SshHostKeys

These values (SSH Host Keys) can be copied from their original location here:

HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys

After this, you’ll want to run plink with the -batch parameter so it does not wait for interactive prompts.

Once I completed a successful test using the TIMEOUT setting in the apcupsd.conf file, I was ready to change that back to 900 (seconds), then set the MINUTES parameter to 5. I unplugged the UPS and sure enough, everything shutdown properly. Now I won’t break a sweat if the power ever goes out for long!