How to configure Preferred Owners and AutoFailback with Powershell in Failover Cluster?

Consider scenario, you “balanced” your VMs across your nodes and you come back in the morning and they are migrated “for whatever reason” to different nodes. But as a typical administrator ;) you know your workload best and you want to keep them preferred on a targeted node. In case of an unexpected reboot the VMs will get migrated to next available node in your Failover Cluster and goal is as soon as the node is back the VM should automatically fail back = PreferredOwners. 

Here is a quick example where you configure all clustered VM roles in a targeted cluster for preferred owners based on the current owner node. this should only give you an idea how things can be automated very easily with Powershell:

$Cluster = Read-Host "Cluster Name "
$Praefix = Read-Host "Please provide präfix for your clustered VM role names (a.e.SCVMM) "
Write-Host " "
Write-Host "Getting all clustered VM roles and configure Preferred Owners in cluster $Cluster" -ForegroundColor yellow
Write-Host " "
Write-Host " "
$AllClusterGroup = Get-ClusterGroup -Cluster $Cluster -Name $Praefix*

Write-Host "....running loop for all Clustered VMs"
Write-Host " "
Write-Host " "
foreach ($ClusterGroup in $AllClusterGroup)
{
$ClusterGroupDetails = Get-ClusterGroup -Name "$ClusterGroup"
Write-Host "Getting current Owner for $ClusterGroup...." -ForegroundColor yellow
$CurrentOwner = $ClusterGroupDetails.OwnerNode.Name
Set-ClusterOwnerNode -Group $ClusterGroup $CurrentOwner
Write-Host "Current owner for VM $ClusterGroup is $CurrentOwner, configure Preferred Owner...done" -ForegroundColor yellow
#Enable Autofailback
(Get-ClusterGroup -Name "$ClusterGroup").AutoFailbackType=1
Write-Host "Enabling Autofailback VM $ClusterGroup...done" -ForegroundColor yellow
Write-Host " "
}

Disclaimer: Please read and test script before you run in your production, this reconfigures the preferred owner property at all your clustered VM roles in your cluster !!

There are more options how you can control clustered roles, like “AntiAffinityClass” – when a group is moved during failover, anti-affinity affects the algorithm used to determine the destination node. a.e. never run together at same node…

Here are just a few more good resources around failover options in a Failover Cluster:

Preferred Owners in a Cluster
https://blogs.msdn.com/b/clustering/archive/2008/10/14/9000092.aspx

Failover behavior on clusters of three or more nodes
https://support.microsoft.com/kb/299631/en-us

Understanding Hyper-V Virtual Machine (VM) Failover Policies
https://blogs.msdn.com/b/clustering/archive/2010/12/14/10104402.aspx

Modify the Failover Settings for a Clustered Service or Application
https://technet.microsoft.com/en-us/library/cc771809.aspx

Configure Failover and Failback Settings for a Clustered Service or Application
https://technet.microsoft.com/en-us/library/dd197473(v=ws.10).aspx

AntiAffinityClassNames
https://msdn.microsoft.com/en-us/library/aa369651(v=vs.85).aspx

Using Guest Clustering for High Availability
https://technet.microsoft.com/en-us/library/dn440540.aspx