247-IT Guide
UPS shutdown via PowerShell script: what is missing in a real outage
A script that shuts down the Hyper-V hosts during a power outage is written quickly. Which weak spots only show up in a real outage, what a shutdown script must do at a minimum, and when a script is still enough.
Twenty lines of PowerShell
Many Hyper-V environments are protected against power outages by a home-grown script. That is not a bad start: query the UPS via NUT or SNMP and shut the hosts down with PowerShell, all in twenty lines. It usually looks something like this:
# Typical shutdown script: query NUT, shut everything down on battery
$ups = 'nas01' # NUT server
$hosts = 'hv01', 'hv02' # Hyper-V hosts
while ($true) {
$tcp = [Net.Sockets.TcpClient]::new($ups, 3493)
$io = $tcp.GetStream()
$w = [IO.StreamWriter]::new($io); $r = [IO.StreamReader]::new($io)
$w.WriteLine('GET VAR ups ups.status'); $w.Flush()
$status = $r.ReadLine() # e.g. VAR ups ups.status "OB DISCHRG"
$tcp.Dispose()
if ($status -match '\bOB\b') {
foreach ($h in $hosts) {
Invoke-Command -ComputerName $h -ScriptBlock {
Get-VM | Where-Object State -eq 'Running' | Stop-VM
Stop-Computer -Force
}
}
break
}
Start-Sleep -Seconds 30
}
In a test it works. In a real power outage it fails for reasons you cannot see in the script.
What goes wrong in a real outage
| Weak spot | Consequence |
|---|---|
| A single reading triggers | A UPS self-test or one second of grid fluctuation shuts everything down. |
| UPS unreachable | The exception ends the script. From then on nobody is monitoring, and nobody notices. |
| Status only, no runtime | The script shuts down immediately, even if the battery would last another hour. |
| No timeout per VM | A VM stuck in Windows updates blocks all following VMs and hosts. |
| Hosts one after another instead of in parallel | The second host only starts once the first is done. The UPS time budget then runs out. |
| The monitoring computer shuts itself down | If the script runs on hv01, it ends itself before hv02 gets its turn. |
| Credentials | The script runs as a scheduled task under a domain admin, often with the password in plain text. |
| No alert, no log | After the outage nobody knows whether or when the script ran. |
| New VMs and hosts | A new host is not added to $hosts and stays unprotected at the next outage. |
| No test mode | Testing means really shutting everything down. So nobody tests. |
What a shutdown script must do at a minimum
- Confirm repeatedly — trigger only after several consecutive readings on battery, and check once more just before shutting down whether power is back.
- Thresholds — evaluate battery charge and runtime, not just the status.
- Handle errors — report an unreachable UPS instead of ending the script. And monitor the script itself: is it still running at all?
- Timeout per VM — afterwards switch off hard with
Stop-VM -TurnOffso a stuck VM does not hold everything up. - Hosts in parallel, own computer last — the monitoring computer only shuts down once all others have their command.
- Protect credentials — a dedicated account with the required rights, the password encrypted with DPAPI or a group managed service account.
- Log and alert — write every step to the event log and notify on power loss, shutdown and power return.
- Test mode — a dry run that only logs what would happen.
Twenty lines quickly become several hundred. They need maintenance whenever hosts, passwords or the UPS change, and usually only one person knows the script.
When a script is enough
- A single host — without an order across several hosts the script stays manageable. Often Hyper-V's stop action is enough then, see Shutting down Hyper-V VMs in a power outage.
- Test environment or lab — where an unnecessary shutdown bothers nobody.
- Someone maintains it — with version control, documentation and a yearly test.
The points above in the UPS Hyper-V Shutdown Monitor
| Requirement | Implementation |
|---|---|
| Repeated confirmation | Triggers only after several readings on battery (default: 3 × 30 s), then a 15 s cancel window if power returns |
| Thresholds | Battery charge in percent and runtime in seconds, set separately |
| Handle errors | Alert when the UPS becomes unreachable and when it is reachable again |
| Timeout per VM | Shut down or save, switch off hard when the timeout expires (default 120 s, set per host) |
| Hosts and order | Order per host: hosts with the same value in parallel, groups one after another, own computer last |
| Power returns | If mains power returns after a triggered shutdown, the monitor re-arms for the next outage (Windows event 2002) |
| Live migration and additional servers | Live migration to a partner host still on mains instead of a shutdown; additional servers with their own, earlier runtime threshold — from Pro |
| Credentials | Passwords encrypted with DPAPI, control via WinRM without an agent |
| Log and alert | Log, shutdown history, email and SNMP trap; Teams, SMS and webhook from Pro |
| Test mode | Dry run without commands to the hosts, logging order and timing |
The software runs as a Windows service without a signed-in user. New VMs on a configured host are shut down automatically as well.
Shutdown checklist
- Does not trigger on a single reading
- Reports when the UPS is unreachable
- Stuck VMs do not hold up the shutdown
- All hosts fit into the UPS time budget
- Notifies on power loss, shutdown and power return
- Was tested in the last year, dry and for real
Frequently asked questions
Can I shut down Hyper-V hosts during a power outage with a PowerShell script?
Yes. The script queries the UPS via NUT or SNMP and shuts down VMs and hosts with Invoke-Command. To work reliably in a real outage, though, it needs repeated confirmation, error handling, timeouts per VM, logging and alerting.
Why is querying the “on battery” status not enough?
Because every self-test and every brief grid fluctuation then triggers a shutdown, and because the script shuts down immediately even if the battery would last a long time. Several consecutive readings and thresholds for battery charge and runtime make sense.
How do I stop a stuck VM from holding up the shutdown?
With a timeout per VM. If the VM is still running afterwards, it is switched off hard with Stop-VM -TurnOff.
Where should the shutdown script run?
On a computer on the same UPS that shuts down last. If it runs on one of the hosts, that host must be at the end of the order.
More guides
UPS alerts via Microsoft 365 and Teams
Graph instead of SMTP AUTH, Workflows instead of connectors.
Hyper-V on Server Core
Protecting hosts without a desktop via WinRM or a web interface.
CyberPower and Riello for Hyper-V
PowerPanel Business and PowerShield³ compared.
Replacing UPS batteries
Ageing, warning signs and swapping without a protection gap.
Restarting after a power outage
Starting servers, storage and VMs automatically when power returns.
Eaton IPM alternative for Hyper-V
Node licensing, editions and Hyper-V integration compared.
Shutting down Hyper-V VMs in a power outage
Stop action, shutdown order and time budget for an orderly shutdown.
SNMPv3 on a UPS network card
authPriv with SHA and AES instead of a plain-text community.
NUT on Windows
Query a UPS via Network UPS Tools from a NAS or Linux server.
Hyper-V failover cluster power outage
Coordinating quorum, CSV and live migration.
Active Directory & power outages
The USN rollback myth and the real risks for domain controllers.
SQL Server & Exchange on Hyper-V
Protecting databases during a power outage.
Calculating UPS runtime
Why the datasheet runtime rarely holds.
PowerChute alternative for Hyper-V
UPS shutdown without vendor lock-in.
Monitoring UPS units from multiple vendors
One tool instead of several silos.
Instead of a script: ready, tested, reliable
30 days free, every feature included. Repeated confirmation, timeouts, order, alerts and test mode, without a single line of PowerShell.