Powershell script to Live Migrate Hyper-V Virtual Machines back to it's Preferred Owner in a Hyper-V Cluster.

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.
"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.


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

Tags: , ,

Catergories: Windows | HyperV | Powershell

Comments (5) -

Stefan Switzerland
August 21, 2011 20:50 CST

greate script!

i change the static cluster-name to a variable.
## Get Cluster Name
$Cluster = Get-Cluster

#change Hyper-V => $Cluster
$colItems = Get-ClusterOwnerNode -Cluster $Cluster -Group $VMGroup

Stan Czerno United States
August 22, 2011 03:13 CST

Ah, good catch...I tried to make it as generic as I could but I forgot that piece.

Tom Waarsenburg Netherlands
June 12, 2012 22:03 CST

Thank You for the great script.
i've Modified the script for a 3 node cluster.:

------------------script snip-------------------------------------------

## Upon Startup of the Hyper-V Host, 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.

## Let me know if you have a better way of doing this as I am not a very good developer and new to Powershell.

## Credits: pdconsec.net/.../...rate-VMs-On-Host-Shutdown.aspx


#clustername ophalen
[string]$cluster = Get-Cluster

#clusternodes ophalen
$ClusterNodes = Get-ClusterNode

foreach ( $clusternode in $clusternodes )

[string]$clusternode = $clusternode

  ## Get all the VMs in the Cluster on the Other Node
  $VMGroups = Get-ClusterNode $clusternode | Get-ClusterGroup | ?{ $_ | Get-ClusterResource | ?{ $_.ResourceType -like "Virtual Machine" } }
        foreach ( $VMGroup in $VMGroups )
                                        write-host "*****************************"
          write-host "VM : $VMGroup"
          $O = 0
          ## Get the Preferred Owner of the VM
          $colItems = Get-ClusterOwnerNode -Cluster $cluster -Group $VMGroup
          [string]$strONodes = $colItems.OwnerNodes

          foreach ($ONode in $colItems.OwnerNodes)
          $DoMigrate = ""
            ## If there is more than one Preferred Owner, do not migrate
            if ($O -gt 1)
              write-host "$VMGroup has more than one Preferred Node: $strONodes"
              write-host "$VMGroup will not be migrated..."
              write-host "If you want $VMGroup to have only one Preferred Node, uncheck all but the one Node"
              $DoMigrate = "No"
            ## If there is only one Preferred owner, grab the Node Name
            elseif ($O -eq 1)
            $DoMigrate = "Yes"
            [string]$pf = $colItems.OwnerNodes -Replace " ", ""

          [string]$currentVM = $VMGroup
          $Currentowner = Get-ClusterGroup -name $currentVM
          $Currentowner = $Currentowner.ownernode
          [string]$strCurrentOwner = $Currentowner
          ## Check the Node Status
          $nodestate = (get-clusternode -Name $pf)
          $nodestate = $nodestate.State
          [string]$strnodestate = $nodestate
          ## If the Current Owner is the Preferred, there is nothing to do.
          if ($strCurrentOwner -eq $pf)
            write-host "$VMGroup is on $Currentowner and will stay on $pf..."
          ## If the Current Owner is not the Preferred Owner, and the Preferred Owner is up, Live Migrate it back to the Preferred Owner.
          If ( $strCurrentOwner -ne $pf )
            write-host "$VMGroup is on $Currentowner but should be on $pf..."
            If ($strnodestate -eq "Up" -and $DoMigrate -eq "Yes")
            write-host "The Node $pf is Up"
            write-host "Live Migrate $VMGroup to Node $pf"
            Move-ClusterVirtualMachineRole $VMGroup -Node $pf
            write-host "*****************************"

----------------------------------------end script snip ----------------------------------------------------------------------------------

August 21, 2014 06:17 CST


How easy would it be to amend this for a 4 node cluster? 2012r2?


Stan Czerno
August 28, 2014 08:41 CST

I have it running on a three node cluster without any issues. You'll just have to test it out and see since I do not have more than three currently.

Comments are closed