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

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.
--------snip----------
## Get Cluster Name
$Cluster = Get-Cluster

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

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

ImportSystemModules

#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)
          {              
                
          $O++
          $DoMigrate = ""
          }
            ## If there is more than one Preferred Owner, do not migrate
            if ($O -gt 1)
            {
              write-host
              write-host "$VMGroup has more than one Preferred Node: $strONodes"
              write-host
              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
            write-host "$VMGroup is on $Currentowner and will stay on $pf..."
            write-host
          }
          
          ## 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..."
            write-host
            If ($strnodestate -eq "Up" -and $DoMigrate -eq "Yes")
            {
            write-host
            write-host "The Node $pf is Up"
            write-host
            write-host "Live Migrate $VMGroup to Node $pf"
            write-host
            Move-ClusterVirtualMachineRole $VMGroup -Node $pf
            write-host
            write-host "*****************************"
            }
          }
        
        }
  }


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

luke
August 21, 2014 06:17 CST

Hi,

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

Thanks,

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