search
top

Using PowerShell to Move Cluster Resources to Preferred Node on Windows Server 2008 and above

Introduction

In an earlier post we covered Using PowerShell to Failover Cluster Resources for Patching, to expand on the script we are adding a third option for use on active / active clusters to move the Cluster Resources to their preferred node if it is defined. We will cover creating the move by itself and also incorporating it in the script covered in the earlier post.

About the Script

The purpose of the PowerShell script is to move cluster resource to their preferred owners.  While doing this to also create a local log file  and also to create a Event in the Application event log. Using this script you can automate patching of clusters with SCCM and other methods. Another note is that the script be able to work on Windows Server 2008, 2008 R2, 2012, 2012 R2  and 2016 versions.

To achieve this we will create a function called balance. In an earlier post we created a script to perform drain and pullall at the PowerShell command line. For this PowerShell cluster operations we will be using the FailOverClusters module and several get cmdlets such as Get-ClusterGroup, Get-ClusterResource, Move-ClusterGroup, Get-ClusterOwnerNode and Get-ClusterNode.

For the logging events portion we will be capturing who is doing the failover, the date and time and what operation is performed into the local logfile stored in c:\support directory using Out-File and Add-Content cmdlets. For the Event logging we will use the Write-EventLog cmdlet and create the event in Application Log. Below is the script and is fairly easy to read and can be modified to your liking.

The Script

In this example we are creating a function called balance and will output the contents to a logfile in c:\support, you can change to whatever you want.

Function Balance {

# Create Local logfile
 $node = hostname
 $date1=Get-Date -Format "yyyy-MM-dd hh:mm tt"
 $clustervip=Get-Cluster
 $clustervipname=($clustervip.Name)
 $admin = (Get-Content -Path env:username).ToString()
 $LogFileName = "c:\Support\ClusterResMove-"+ $node +".txt"
 Out-File -append -FilePath $LogFileName -Encoding ASCII
 Add-Content -Path $LogFileName -Value "Computer: $node" -Encoding ASCII
 Add-Content -Path $LogFileName -Value "*****************************" -Encoding ASCII

Import-Module FailoverClusters
 Add-Content -Path $LogFileName -Value "Date: $date1" -Encoding ASCII
 Add-Content -Path $LogFileName -Value "Executed By: $admin" -Encoding ASCII
 Add-Content -Path $LogFileName -Value "Current Cluster Assignments" -Encoding ASCII
 $computer = get-content env:computername
 $computer = $computer.ToLower()

Get-ClusterGroup | Out-File -append -Encoding ASCII -FilePath $LogFileName

$clustergroups = Get-ClusterGroup | Where-Object {$_.IsCoreGroup -eq $false}
 foreach ($cg in $clustergroups)
    {
     $CGName = $cg.Name
      Add-Content -Path $LogFileName -Value "Getting Cluster Group Owners" -Encoding ASCII
      $CurrentOwner = $cg.OwnerNode.Name
       $POCount = (($cg | Get-ClusterOwnerNode).OwnerNodes).Count
 if ($POCount -eq 0)
    {
    Add-Content -Path $LogFileName -Value "Info: $CGName doesn't have a preferred owner!" -Encoding ASCII
     }
    else
    {
    $PreferredOwner = ($cg | Get-ClusterOwnerNode).Ownernodes[0].Name
     Add-Content -Path $LogFileName -Value "$PreferredOwner" -Encoding ASCII
    if ($CurrentOwner -ne $PreferredOwner)
    {
     Add-Content -Path $LogFileName -Value "Moving resource to $PreferredOwner" -Encoding ASCII
      $cg | Move-ClusterGroup -Node $PreferredOwner
     }
      else
     {
      Add-Content -Path $LogFileName -Value "Resource is already on preferred owner! ($PreferredOwner)" -Encoding ASCII
       }
     }
  }
   Add-Content -Path $LogFileName -Value "Cluster Balance Operation Completed" -Encoding ASCII
   Add-Content -Path $LogFileName -Value "New Cluster Assignments" -Encoding ASCII
   Get-ClusterGroup | Out-File -append -Encoding ASCII -FilePath $LogFileName
 }

The script queries the objects in the cluster for their owners and migrates the resources to them until all have moved. If the resource is on the preferred owner the script does nothing. This is a very handy function to use when patching active / active node clusters with 2 or more nodes to move resources after patching. Add this function with the previous post and you have a very usable script for all needs.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

top