by Stan Czerno
July 21, 2011 13:15 CST
I schedule this script to run at Startup of the Hyper-V Host to check to see if VMs have been Live Migrated to another Host.
If the VM has this Host set as being the Preferred Owner, Live Migrate them back to this Node.
I set the scheduled task as:
Security: Run whether the user is logged on or not, Run with highest privileges.
Trigger: "At startup" and delay it for 5 minutes.
Action: "Start a program", Program/Script: powershell -command "& 'C:\scripts\movebacktopreferred.ps1'"
This script is only designed to work with a Two-Node Cluster but it is a good starting point if you have more than a Two Node Cluster. Another foreach will need to be used for the multiple nodes in the Cluster.
http://www.czerno.com/default.asp?inc=/html/windows/hyperv/cluster/HyperV_Live_Migrate_VMs_back_to_Preferred_Owner.asp
Let me know what you think about the script and if you have a better way of doing this.
Windows Server 2012:
Windows Server 2012 includes new Powershell cmdlets and cmdlet features. Assuming you drained the VMs at shutdown and using the same methodology as above, all you need to include in your script for Windows Server 2012 is the following:
## Get this Node's Name
$Computer = Get-Content env:ComputerName
## Suspending Cluster Node
Suspend-ClusterNode $Computer
## Sleeping for 20 seconds
Start-Sleep -s 20
## Resuming Cluster Node and Failing Back VMs
Resume-ClusterNode $Computer -Failback Immediate