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
Pantelis Apostolidis is a Sr. Specialist, Azure at Microsoft and a former Microsoft Azure MVP. For the last 20 years, Pantelis has been involved to major cloud projects in Greece and abroad, helping companies to adopt and deploy cloud technologies, driving business value. He is entitled to a lot of Microsoft Expert Certifications, demonstrating his proven experience in delivering high quality solutions. He is an author, blogger and he is acting as a spokesperson for conferences, workshops and webinars. He is also an active member of several communities as a moderator in azureheads.gr and autoexec.gr. Follow him on Twitter @papostolidis.