by Stan Czerno
July 21, 2011 13:09 CST
On a planned or gracefull Hyper-V Host Shutdown, Live Migrate VMs to another Node in the Cluster. This Script is designed to work with a Two-Node Cluster.
You need to configure Group Policy or the Local Policy for the host to have a Shutdown Script. You'll find these settings under Computer Settings > Windows Settings > Scripts (Startup/Shutdown).
Using the PowerShell Scripts tab, set PowerShell scripts to run First so that the live migrations are performed before shutdown.
Let me know what you think about the script and if you have a better way of doing this.
http://www.czerno.com/default.asp?inc=/html/windows/hyperv/cluster/HyperV_Live_Migrate_VMs.asp
Windows Server 2012 NOTE: If you are using Windows Server 2012 or Hyper-V Server 2012 you will need to make a change in the registry to make this work. Open regedit, then navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control. Modify PreshutdownOrder and remove vmms.
Speaking of Windows Server 2012, you do not even need to use this script. Windows Server 2012 includes new Powershell cmdlets and cmdlet features. Below is all you need for a script in a Windows Server 2012 Cluster, regardless of how many Nodes:
##Get this Node's Name
$Computer = Get-Content env:ComputerName
##Suspend this Node so nothing gets migrated to it
write-host "This computer: " $Computer
write-host
Write-Host "Suspending Cluster Node and Draining VMs"
Suspend-ClusterNode $Computer -drain -wait