Auto-Shutdown Hyper-V free with UPS

Auto-Shutdown Hyper-V free with USB UPS

This post is about how to Auto Shutdown Hyper-v core with UPS using PowerShell and also send email notifications based to UPS battery level.

Recently i installed a Windows Hyper-V 2012 R2 server (the free version) but my UPS doesn’t support Windows Core. No problem, we have PowerShell!! after some search on various sites – blogs – etc i end up creating the following script. It checks the battery status every 3 minutes, using WMI and when the battery drops below 50% is sends the shutdown signal. As long as you set the VMs to save on shutdown you are OK!

I also added a simple mail notification before the shutdown.

$batterystatus = (get-wmiobject -class CIM_Battery -namespace "root\CIMV2").EstimatedChargeRemaining
DO
{
 start-sleep -seconds 180
 $batterystatus = (get-wmiobject -class CIM_Battery -namespace "root\CIMV2").EstimatedChargeRemaining
 $batterystatus
 } While ($batterystatus -gt 50)
$login = "username"
$password = "password" | Convertto-SecureString -AsPlainText -Force
$credentials = New-Object System.Management.Automation.Pscredential -Argumentlist $login,$password
Send-MailMessage -Body "UPS Started - Server will shutdown in 5 minutes" -From [email protected] -To [email protected] -Subject "Power Loss - UPS Started" -SmtpServer mail.domain.com -Credential $Credentials
shutdown /s /t 300

Feel free to use my script and also edit at will based to your needs

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.