How to Rebalance Mailbox Databases in a DAG with Exchange Server 2010 and 2013

After an outage or maintenance to the Exchange 2010 or Exchange 2013 Mailbox servers in a Database Availability Group you may find that the mailbox databases are no longer balanced across all of the DAG members.

For example after applying updates to DAG members you may see that all of the mailbox databases are active on a single DAG member.

[PS] C:\>Get-MailboxDatabase | ft name, server, activationpreference -AutoSize

Name                Server ActivationPreference
----                ------ --------------------
Mailbox Database 02 EX1    {[EX2, 1], [EX1, 2]}
Mailbox Database 01 EX1    {[EX1, 1], [EX2, 2]}
Mailbox Database 03 EX1    {[EX1, 1], [EX2, 2]}
Mailbox Database 04 EX1    {[EX2, 1], [EX1, 2]}

You can see in the above output that all of the mailbox databases are active on server EX1, even though some of them have EX2 as a preferred server.

Exchange Server 2010 Service Pack 1 shipped with a script that allows you to automatically redistribute mailbox databases to their first activation preference. The script can be found in the \Scripts folder of the Exchange Server 2010 installation path, which by default would be C:\Program Files\Microsoft\Exchange\V14\Scripts.

You can use the script to output a report of the current mailbox database distribution among the DAG members.

[PS] C:\Program Files\Microsoft\Exchange Server\V14\Scripts>.\RedistributeActiveDatabases.ps1 -DagName DAG -ShowDatabase
DistributionByServer | ft

ServerName           TotalDbs      ActiveDbs     PassiveDbs PreferenceCoun     MountedDbs  DismountedDbs DagName
                                                            tList
----------           --------      ---------     ---------- --------------     ----------  ------------- -------
EX2                         4              0              4 {2, 2}                      0              0 dag
EX1                         4              4              0 {2, 2}                      4              0 dag

To rebalance the mailbox databases based on activation preference use the following script parameters.

[PS] C:\Program Files\Microsoft\Exchange Server\V14\Scripts>.\RedistributeActiveDatabases.ps1 -DagName DAG -BalanceDbsBy
ActivationPreference

***************************************
Balance DAG DBs
Thursday, October 28, 2010 10:16:36 PM
***************************************
Dag                                :   dag
ServerCount                        :   2
DatabaseCount                      :   4
CopiesCount                        :   8

----------------------------
Starting Server Distribution
----------------------------

ServerName TotalDbs ActiveDbs PassiveDbs PreferenceCountList MountedDbs DismountedDbs DagName
---------- -------- --------- ---------- ------------------- ---------- ------------- -------
EX1               4         4          0 {2, 2}                       4             0 dag
EX2               4         0          4 {2, 2}                       0             0 dag


-----------------------
Starting Database Moves
-----------------------

Considering move of 'Mailbox Database 04' from 'ex1' (AP = 2) to 'EX2' (AP = 1)...

Confirm
Are you sure you want to perform this action?
Moving mailbox database "Mailbox Database 04" from server "ex1.exchangeserverpro.local" to server
"EX2.exchangeserverpro.local".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "Y"): a
Database 'Mailbox Database 04' successfully moved from 'ex1' to 'ex2'.

ServerName ActiveDbs PassiveDbs
---------- --------- ----------
EX1                3          1
EX2                1          3 `n
Considering move of 'Mailbox Database 02' from 'ex1' (AP = 2) to 'EX2' (AP = 1)...

Confirm
Are you sure you want to perform this action?
Moving mailbox database "Mailbox Database 02" from server "ex1.exchangeserverpro.local" to server
"EX2.exchangeserverpro.local".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "Y"): a
Database 'Mailbox Database 02' successfully moved from 'ex1' to 'ex2'.

ServerName ActiveDbs PassiveDbs
---------- --------- ----------
EX1                2          2
EX2                2          2 `n

----------------
Summary of Moves
----------------
Successfully moved      : 2
Moved to less preferred : 0
Failed to move          : 0
Not moved               : 2

Start time              : Thursday, October 28, 2010 10:16:35 PM
End time                : Thursday, October 28, 2010 10:17:51 PM
Duration                : 00:01:15.3764472



DbName                    : Mailbox Database 01
ActiveOnPreferenceAtStart : 1
ActiveServerAtStart       : ex1
ActiveOnPreferenceAtEnd   : 1
ActiveServerAtEnd         : ex1
IsOnMostPreferredCopy     : True
MoveStatus                : NoMoveAttempted

DbName                    : Mailbox Database 02
ActiveOnPreferenceAtStart : 2
ActiveServerAtStart       : ex1
ActiveOnPreferenceAtEnd   : 1
ActiveServerAtEnd         : ex2
IsOnMostPreferredCopy     : True
MoveStatus                : MoveSucceeded

DbName                    : Mailbox Database 03
ActiveOnPreferenceAtStart : 1
ActiveServerAtStart       : ex1
ActiveOnPreferenceAtEnd   : 1
ActiveServerAtEnd         : ex1
IsOnMostPreferredCopy     : True
MoveStatus                : NoMoveAttempted

DbName                    : Mailbox Database 04
ActiveOnPreferenceAtStart : 2
ActiveServerAtStart       : ex1
ActiveOnPreferenceAtEnd   : 1
ActiveServerAtEnd         : ex2
IsOnMostPreferredCopy     : True
MoveStatus                : MoveSucceeded

The outcome of the moves can be seen here. Each mailbox database in the DAG is now active on its preferred server.

[PS] C:\>Get-MailboxDatabase | ft name, server, activationpreference -AutoSize

Name                Server ActivationPreference
----                ------ --------------------
Mailbox Database 02 EX2    {[EX2, 1], [EX1, 2]}
Mailbox Database 01 EX1    {[EX1, 1], [EX2, 2]}
Mailbox Database 03 EX1    {[EX1, 1], [EX2, 2]}
Mailbox Database 04 EX2    {[EX2, 1], [EX1, 2]}

Comments

  1. M G says

    Hi,
    Thanks Paul
    But how can i make it run automatically ?
    i mean it asks to verify moving databases but i like this script every night run as a scheduled task and fully automatic (i do not want to enter “y” press enter and …)
    i like it to be run without asking for verification

  2. Hein says

    Weird. Can’t see what i am doing wrong. I get this:

    At C:\Program Files\Microsoft\Exchange Server\V14\scripts\RedistributeActiveDatabases.ps1:1928 char:3
    + return $moveSuccessful
    + ~~~~~~~~~~~~~~~~~~~~~~
    Control cannot leave a finally block.
    At C:\Program Files\Microsoft\Exchange Server\V14\scripts\RedistributeActiveDatabases.ps1:2538 char:5
    + return $success
    + ~~~~~~~~~~~~~~~
    Control cannot leave a finally block.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : ControlLeavingFinally

    Hopefully you can shine a light on this and help me out :)

  3. David says

    paul

    can you shed some light…..? IM getting this when i run issue this cmd

    thanks

    .\RedistributeActiveDatabases.ps1 -DagName DAG -ShowDatabase
    DistributionByServer | ft

    [PS] C:\Program Files\Microsoft\Exchange Server\V14\scripts>.\RedistributeActiveDatabases.ps1 -DagName DAG -ShowDatabase
    DistributionByServer | ft
    C:\Program Files\Microsoft\Exchange Server\V14\scripts\RedistributeActiveDatabases.ps1 : Parameter cannot be processed
    because the parameter name ‘ShowDatabase’ is ambiguous. Possible matches include: -ShowDatabaseCurrentActives -ShowData
    baseDistributionByServer -ShowDatabaseDistributionBySite.
    At line:1 char:34
    + .\RedistributeActiveDatabases.ps1 <<<< -DagName DAG -ShowDatabase DistributionByServer | ft
    + CategoryInfo : InvalidArgument: (:) [RedistributeActiveDatabases.ps1], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameter,RedistributeActiveDatabases.ps1

  4. Gary H says

    Thanks Paul.. It works like a charm.

    Is there a Script to Automatically Rebalance going forth? or is the only method to run this rebalance script?

  5. Christian says

    Hi Paul,
    Do you know a way to include an email report the result of the command RedistributeActiveDatabases.ps1 -DagName DAG -BalanceDbsBy ActivationPreference in the script?

  6. Wayne says

    Hi Paul

    What if i have 3 servers in single DAG (All roles installed on each) 2 are active/passive and 3rd is just passive , how do i make sure that the DB’s end up back on the original place and leave the 3rd as passive?

    • says

      There are no active/passive servers in a DAG. There are only active database copies and passive database copies.

      If you want to control which database copy becomes active when the rebalancing script is run as shown above, then you would set that database copy’s activation preference to 1.

  7. Joe Hardy says

    What kind of impact does this have on outlook clients in a 24/7/365 shop? Is there a default level of availability this script uses? ie: “none” – “best availibility”
    Wondering if end users will see any performance hits or disconnects in outlook if we run this cmdlet / script.
    Thanks,
    Joe

    • says

      Good question. I think it honors the setting on the database, but a quick scan through the script code didn’t find where that might be controlled.

      There’s always a risk even during a planned switchover that something will go wrong of course.

  8. Jetty says

    Question – I am not an exchange admin and am working my way through some Exchange items and would like to understand this better. What would happen in the event of a failure if the active database is mounted on the server with the activation preference of 2?

    • says

      In a failure situation a process called Best Copy Selection evaluates a number of factors and that is how the database copy to attempt to bring online is chosen. Activation preference is one of those factors but it is not the only one.

      If you’re asking if its possible that db copy AP=1 could come online, yes that is possible, if Best Copy Selection chooses that copy.

  9. James Slack says

    Hi Paul,

    Is there a way to mount a DB on a specific server in a DAG when it is currently mounted on a different server in that DAG?

    We have 12 DBs and 2 servers (ignoring DR). Say I wish to mount only DB7 on DB2 instead of DB1 – how would I do this?

    The reason I ask is that we get some issues when we have to reboot one of the DAG servers. The DBs should all fail over to the other server, but sometimes one or two get stuck in limbo. This makes the other server take 19 minutes (due exchange services) to boot and those DBs that dont fail over are stuck offline until it comes back. I do the reboot correctly, stopping services etc. first.

    If I can dismount DBs one at a time and mount them on the server I desire, it will be a lot better.

    I believe the mount-database command will just mount the DB in the same place.

    Thanks

  10. Rosario says

    Paul, thanks, another great article. My question:

    In a DAG none of the databases is mounted on one primary server. They are mounted on the two others. So I would like to redistribute or do a manual switch over, but before doing that, I would ask WHY were they mounted on the other two servers leaving that one server only with copies an no active DB?

    So is there a means to know the reason of this switch over? No hardware failure on that server, no Error entries in the Event Viewer. Where else should I investigate?

    Thanks a lot, Rosario

      • Rosario says

        Thanks, I found the Event-Viewer entries and would like to mention them here for reference.

        On the faulty server have a look at Event-Viewer->Applications and Services Logs->Microsoft->Exchange->HighAvailability and same path->MailboxDatabaseFailureItems

  11. quaaarks says

    Hi,
    when I use this script: .\RedistributeActiveDatabases.ps1 -DagName dag1 -ShowDatabaseDistributionByServer | ft
    I get following error:

    The operation couldn’t be performed because object ‘dag1′ couldn’t be found on ‘dc.domain.com’.
    + CategoryInfo : NotSpecified: (:) [Get-DatabaseAvailabilityGroup], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : [Server=EXCH1,RequestId=eea7abfb-5a4a-4885-8740-f96617cb4dc7,TimeStamp=27.03.2014 16
    :03:43] D83A179C,Microsoft.Exchange.Management.SystemConfigurationTasks.GetDatabaseAvailabilityGroup
    + PSComputerName : exch1.domain.com

    Log-Error : [16:03:43.628 UTC] Could not find DAG matching ‘dag1′!
    At C:\Program Files\Microsoft\Exchange Server\V15\Scripts\RedistributeActiveDatabases.ps1:2801 char:3
    + Log-Error ($RedistributeActiveDatabases_LocalizedStrings.res_0089 -f $DagName) …

    I receive these errors with Exchange 2013 as well as in Exchange 2010 Environment?
    I would appreciate any help

  12. Edwin says

    What rights does the account that runs the script need? Our script account recently lost domain admin rights, and keeps failing now.

  13. Gary says

    How would you script moving all databases to a specific server in the DAG instead of the preferred server? For instance, maintenance will be occurring on server 1 in the DAG so in preparation you move all databases to be mounted on server 2 in the DAG.

Leave a Reply

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