Powershell script to move Failover Cluster Instances to their preferred node

Assume you have multiple SQL Server Instances spread over multiple Failover Cluster Nodes (Active/Active), and you want to move the SQL Instances back to their preferred node after maintenance (example: Windows Updates) on the cluster.

You could choose to configure the Failback option, which is build-in. However, this does not give you exact control regarding the failback time. It only allows specification of a daily time window during which failback is allowed, specifying one day is not possible.

So that was not good enough for me. I ended up writing the Powershell script as listed below. Please use at your own risk and test before deployment. Thanks to @sql_williamd @retracement and @sqlfinn for pointing me in the right direction.

# Powershell script to move Failover Cluster Instances to their preferred node # By Hugo Tap # This version only uses the first preffered node, feel free to improve this. # It only does its work on the node which owns the Cluster Group.
# Powershell script to move Failover Cluster Instances to their preferred node
# By Hugo Tap
# This version only uses the first preffered node, feel free to improve this.
# It only does its work on the node which owns the Cluster Group.

Import-Module FailoverClusters
$computer = get-content env:computername
$computer = $computer.ToLower()

Get-ClusterGroup “Cluster Group” |
foreach-object `
{
$owner = “” + $_.OwnerNode
If ($owner -eq $computer) #Only if this node owns the Cluster Group, run the following.
{
Get-ClusterGroup | Get-ClusterOwnerNode |
foreach-object `
{
If ($_.OwnerNodes[0] -ne $null) #Only if the Group has a preffered node
{

Move-ClusterGroup -Name $_.ClusterObject -Node $_.OwnerNodes[0]
}
}
}
}

Advertisements

About sqlsmurf
MS SQL Administrator

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: