Overarching DAG Powershell Maintenance Script…

Recently I have been doing a quite a bit in relation to Database Availability Groups, Service Packs, Roll-Ups in my lab. In order to test my applications (honestly, I do some testing before I release them) – there is quite a lot of shutting down, restarting and generally messing around with my DAG based Exchange Servers.

In order to facilitate all of the above and of course keep my servers in good shape, it has been a requirement to place certain servers within my infrastructure into various forms of DAG maintenance, restore normal operation, and finally redistribute the databases post DAG server maintenance.

Now as many of your will be aware – within Exchange 2010 from SP1 there are a number of built in scripts which administrators can use to configure a number of maintenance tasks on DAG members these typically are:

  • StartDagServerMaintenance.ps1
  • StopDagServerMaintenance.ps1
  • RedistributeActiveDatabases.ps1

Normally the above are found in the C:\Program Files\Microsoft\Exchange Server\v14\Scripts folder

All these scripts are excellent accompaniments to your Exchange infrastructure – but as I have been using all three on a regular basis I decided for convenience to amalgamate them into a single script and add a new “Query” feature.

In essence the script that I have written is an encapsulation layer for all of the above Microsoft supplied ps1 scripts – encapsulating the following features:

  • Accepts a single command line parameter via the –opMode switch:
    • Start – Executes the StartDagServerMaintenance.ps1 script but automatically detects the serverName parameter
    • Stop – Executes the StopDagServerMaintenance.ps1 script but automatically detects the serverName parameter
    • Query – New feature, detects the status of the DAG node that the script is executed on and reports the following information:

dagEncaps001

    • Redist – Executes the RedistributeActiveDatabases.ps1 script but automatically detects the DAGName and sets the –BalanceDBSByActivationPreference parameter

DAG Server Maintenance Script

The following is an overview of the encapsulation script for you to review prior to download.

# Dag-Server-Maintenance Automation Script
# Author: Andy Grogan
# Version 1.0
# www.telnetport25.com

# Command Line Parameters

Param(
    [Parameter(Mandatory=$true)]
    [System.Management.Automation.ValidateNotNullOrEmptyAttribute()]
    [string] $opMode
)

# Query the Registry for the Exchange Install Location

$HKLM = 2147483650
$WMIReg = [wmiclass]'\\.\root\default:StdRegProv'
$specKey = "Software\Microsoft\ExchangeServer\v14\Setup"
$keyVal = "MsiInstallPath"
$retVals = $WMIReg.GetStringValue($HKLM,$specKey,$keyVal)
$exPath = $retVals.sValue


$localServerName = Get-WmiObject -Class Win32_ComputerSystem | select Name

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue
   

function do_functionalTask{
    
    if($opMode.ToLower() -eq "start"){
        Powershell.exe -File $exPath"Scripts\StartDagServerMaintenance.ps1" -serverName $localServerName.Name
    }elseif($opMode.ToLower() -eq "stop"){
        Powershell.exe -File $exPath"Scripts\StopDagServerMaintenance.ps1" -serverName $localServerName.Name
    }elseif($opMode.ToLower() -eq "query"){
    
        $AP = Get-MailboxServer -Identity $localServerName.Name | Select DataBaseCopyAutoActivationPolicy
        $CS = Get-MailboxDatabaseCopyStatus -Server $localServerName.Name | Select Name,ActivationSuspended
        Write-Host "------------------------------------------------"
        Write-Host "Activation Policy: " $AP.DataBaseCopyAutoActivationPolicy -ForegroundColor Cyan
        Write-Host "Copy Status for Server: " $localServerName.Name -ForegroundColor Yellow
        Write-Host "------------------------------------------------"
        foreach($val in $CS){
            Write-Host "Database Name: " ($val).Name "`nIs Activation Suspended?: " ($val).ActivationSuspended -ForegroundColor Green
        }
        Write-Host "------------------------------------------------"
        Write-Host "Cluster Node Status: "
        Cluster.exe node $localServerName.Name
        Write-Host "------------------------------------------------"
    }elseif($opMode.ToLower() -eq "redist"){
        
        $DAGName = Get-DatabaseAvailabilityGroup | Where {$_.Servers -eq $localServerName.Name} | Select Name
        Powershell.exe -File $exPath"Scripts\redistributeActiveDatabases.ps1" -DagName $DAGName.Name -BalanceDBSByActivationPreference -ShowFinalDatabaseDistribution
    
    }else{
        Write-Host "Error: Sorry the script does not support the command line argument you supplied." -ForegroundColor Red
        Write-Host "Valid Command Line Parameters are:" -ForegroundColor Cyan
        Write-Host " -opMode Start" -ForegroundColor Green
        Write-Host " -opMode Query" -ForegroundColor Green
        Write-Host " -opMode Stop" -ForegroundColor Green
        Write-Host " -opMode Redist" -ForegroundColor Green
    }
}
do_Functionaltask $opMode

    

Script Requirements

In order to use the script within your environment you will need to ensure that it (sic. the environment) conforms to the following requirements:

NOTE: As the script is designed for Mailbox Servers which are part of a DAG topology the chances are that you will automatically comply with the requirements below.

  • Powershell version 2.0
  • Exchange Server 2010 SP1 or above
  • Exchange Server Management Tools installed
  • Must be executed on the DAG Server that you wish to place into / take out of maintenance mode

Download

You can download the above script from the download link below – you should place the script in a convenient location on your DAG servers.

[ DagServerMaintenance.ps1 – 2KB ]

Script Usage

The script is executed from the Powershell Command Prompt (you do not need to have to use the Exchange Management Console). The script accepts a single parameter called “-opMode” which accepts one of four parameters (these are described above – and illustrated below):

dagEncaps002

-opMode Start

Example of output from using the –opMode Start Command;

dagEncaps003

-opMode Stop

Example of output from using the –opMode Stop Command;

dagEncaps004

-opMode Query

Example of output from using the –opMode Query Command;

dagEncaps006

-opMode Redist

Example of output from using the –opMode Redist Command;

dagEncaps005

I hope that someone will find this useful.

Sharing is caring!:

5 thoughts to “Overarching DAG Powershell Maintenance Script…”

  1. I have four member DAG and I want to install Exchange 2010 SP2 on all four nodes one at a time. Should I run the “-opMode Redist” on each node after completing the installation. Please clarify…
    Thanks,
    GQUre

    1. You should run the Redist command after you have completed the upgrades. The purpose of the REDIST switch is to redistribute the mounted databases within your DAG so that they are balanced – this step is optional and you are, of course free to manually redistribute the databases as you need.

  2. Hi Andy Grogan,
    Thanks for your quick response. My question is should i run REDIST switch after completing the upgrade on all four nodes? I will be installing SP2 on all DAG members one at a time. I will do on server1, server2, so on.. Once i finished server1, I would take this server out of maintenance mode by using switch “-OpMode Stop” and soon after this should i run the REDIST switch on this server? Or just upgrade all four nodes with SP2 and then go to any node and run REDIST switch in order to rebalance all the databases on their respective hosting servers with activation preference.

  3. Hello Andy,
    Firstly thank you for your contribution to the community, it is much appreciated. I’m wondering how difficult it would be to add an option for tacking on -overrideMinimumTwoCopies to the StartDagServerMaintenance.ps1 (which was introduced in SP2 RU1 so the script does not fail where there are only two databases in DAGs with 3+ members). I was just going to add it in as an additional parameter to avoid the extra code of detecting SP2 RU1. Thoughts?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.