Rebalance DAG automatically using Task Scheduler

Those of you who manage Exchange Database Availability Groups, particularly in Exchange 2013, will understand the frustration of coming to the office on a Monday morning to find that your Databases have migrated to A. N. Other server because of some scheduled backup or other task which occurred over the weekend.

To solve this problem, we can configure a Scheduled Task to run the RedistributeActiveDatabases.ps1 script, which will balance your databases out based on the Activation Preference set for each server. Before you configure this task, make sure that your Activation Preference settings are applied as per your desired configuration.

For example, say I have a database named MBDB and two servers named Server1 and Server2 (I’m feeling particularly inventive today). To set the Activation Preference for these database copies, I would run the following Powershell command from EMS:

Set-MailboxDatabaseCopy -identity MBDB\Server1' -ActivationPreference 1
Set-MailboxDatabaseCopy -identity MBDB\Server2' -ActivationPreference 2

This configuration will mean that when I run the RedistributeActiveDatabases.ps1 script, the MBDB database will be moved, if required, to Server1, as long as the required Database Mount Dial setting is achieved (the default setting is Best Availability which requires the Copy Queue Length to be no more than 12).

If I wanted to see how my database copies are currently configured, I would run the following Powershell command from EMS:

Get-MailboxDatabase MBDB | fl server, databaseCopies, activationPreference

This would show me all the database copies and their Activation Preference. So we now have our settings configured and we want to setup our Scheduled Task. Open your Task Scheduler and create a new Basic Task. Give the task a name and configure your recurrence schedule for the Task. When you get to the Action menu, choose ‘Start a Program’.

Now we need to tell the task to:

1. Launch Powershell
2. Load the Exchange modules and connect to the Exchange server
3. Run the RedistributeActiveDatabases.ps1 script with the correct switches
4. Supress the confirmation dialog which appears when running the script

To do this, we enter the details as follows:

  • Program/script:
    • C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe
  • Add arguments:
    • -NonInteractive -WindowStyle Hidden -command “. ‘C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; .’C:\Program Files\Microsoft\Exchange Server\V15\scripts\RedistributeActiveDatabases.ps1’ -DagName DD-DAG -BalanceDbsByActivationPreference -Confirm:$false”
    • Note: Please change the DagName parameter to reference your own DAG!

It looks a little bit like this (OK, it looks just like this):

TaskScheduler DAG Rebalance

Complete the task creation and then go into the properties of the task. On the General page, configure the task to:

1. Run whether the user is logged on or not
2. Run with highest privileges
3. If need be, change the account running the task to a service account of some kind. That account must be a member of the Organisation Management group

Just like this:

TaskScheduler DAG Rebalance 2

Your Scheduled Task is not configured. You can test it out by manually running it. Hopefully this solves a headache for one or two of you out there!

5 thoughts on “Rebalance DAG automatically using Task Scheduler

  1. Ken says:

    your scheduled task action argument is missing a double quote at the end. Should look like this:

    -NonInteractive -WindowStyle Hidden -command “. ‘C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; . ‘C:\Program Files\Microsoft\Exchange Server\V15\scripts\RedistributeActiveDatabases.ps1’ -DagName DAGNAME -BalanceDbsByActivationPreference -Confirm:$false “

    Like

  2. Wal says:

    Nice, thanks for this.

    A word of caution, I copied and pasted the command argument straight from this page and the quote marks were from a different character set and didn’t work when I pasted them in to Task Scheduler.

    Like

  3. Rob says:

    Task doesn’t work for me. Running the entire line manually like this gives me an error:

    C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -NonInteractive -command “. ‘D:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto;. ‘D:\Program Files\Microsoft\Exchange Server\V14\Scripts\RedistributeActiveDatabases.ps1’ -DagName mydag -BalanceDbsByActivationPreference -confirm:$false”

    Error:
    Cannot process argument transformation on parameter ‘Confirm’. Cannot convert value “System.String” to type “System.Management.Automation.SwitchParameter”, parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.

    Have tried the different values with no luck. Running the entire command manually without the Confirm switch works fine. The confirm switch does work if I run this alternative manual command to achieve the same thing:

    CD $Exscripts
    .\RedistributeActiveDatabases.ps1 -DagName mydag -BalanceDbsByActivationPreference –confirm:$false

    I can’t however get this 2nd command working by wrapping it into a batch file and running that using a task. Tearing my hair out!

    Like

    • Odd. Unfortunately I don’t have any access to the environment that I built this for, so I can’t check my syntax. Judging from the error it doesn’t think that anything is specified under the confirm parameter which is strange. Generally when I find an error which doesn’t appear to make any sense, I retype that part of the command using notepad to remove any possible formatting errors.

      Like

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