Forum Discussion
PS script for moving clustered VMs to another node
Windows Server 2022, Hyper-V, Failover cluster
We have a Hyper-V cluster where the hosts reboot once a month. If the host being rebooted has any number of VMs running on it the reboot can take hours. I've proven this by manually moving VM roles off of the host prior to reboot and the host reboots in less than an hour, usually around 15 minutes.
Does anyone know of a powershell script that will detect clustered VMs running on the host and move them to another host within the cluster? I'd rather not reinvent this if someone's already done it.
1 Reply
You don't need to enumerate the VMs yourself. Failover Clustering can drain the node and live-migrate its clustered roles before the reboot:
Import-Module FailoverClusters
Suspend-ClusterNode -Name "HV01" -Drain -Wait
If everything must go to one host, add -TargetNode "HV02"; otherwise the cluster selects suitable owners. Before rebooting, confirm this returns no VM groups:
Get-ClusterGroup | Where-Object { $_.OwnerNode -eq "HV01" -and $_.GroupType -eq "VirtualMachine" }
After the server returns, run:
Resume-ClusterNode -Name "HV01" -Failback NoFailback
Use Immediate instead of NoFailback only if you intentionally want workloads moved back. Also verify the remaining nodes have enough CPU and memory capacity before draining.