Exchange 2010/2013: Distribute databases to the corresponding servers via script

In Exchange environments with DAG and multiple database copies, the databases should normally be distributed evenly across the database servers. However, this situation can change if a failover has occurred or the databases have been activated on other servers for maintenance purposes. At some point, you may find yourself in a situation where all the databases on one server are active and the other servers are bored.

This little script provides a remedy:

#www.FrankysWeb.de
#Frank Zöchling
#---------------------------------
$From = "DBMover@frankysweb.de"
$To = "frank@frankysweb.de"
$SMTPServer = "smtp.frankysweb.de"
#---------------------------------
$exsnapin = Add-PSSnapin *exchange*
$Databases = Get-MailboxDatabase
$DBStatus = @()
[string]$mail = "<b>The mailbox server for the following databases has been changed</b>
"
foreach ($database in $databases)
{
$DBName = $database.Name
$DBPreferedServer = ($database.ActivationPreference | where {$_.value -eq "1"}).key.name
$DBCurrentServer = ($database.Server).name
if ($DBPreferedServer -ne $DBCurrentServer)
{
write-host "Active mailbox server for database $DBName is changed to server $DBPreferedServer (was active on $DBCurrentServer)" -ForegroundColor yellow
$MoveDB = Move-ActiveMailboxDatabase $DBName -ActivateOnServer $DBPreferedServer
$DBStatus += new-object PSObject -property @{Database="$DBName";DBPreferedServer="$DBPreferedServer";DBCurrentServer="$DBCurrentServer";DatabaseMove="yes"}
}
else
{
write-host "Database $DBName is active on the designated server" -ForegroundColor green
$DBStatus += new-object PSObject -property @{Database="$DBName";DBPreferedServer="$DBPreferedServer";DBCurrentServer="$DBCurrentServer";DatabaseMove="no"}
}
}
if ($DBStatus.databasemove -contains "yes")
{
$DBmoved = $DBStatus | where {$_.Databasemove -match "yes"}
[string]$mail += $DBmoved | ConvertTo-Html
Send-MailMessage -From $from -To $to -SmtpServer $SMTPServer -Subject "Mailbox databases redistributed" -Body $mail -BodyAsHTML -Encoding UTF8
}

The script distributes all databases to the corresponding servers. The script can be executed manually or via the task scheduler. As soon as the databases have been distributed according to their preference, a notification email is sent.

Only the recipient, sender and SMTP server need to be specified in the upper part of the script. This setting can be used via the task scheduler to execute the script at a selected time:

Databases

4 thoughts on “Exchange 2010/2013: Datenbanken per Script auf die entsprechenden Server verteilen”

  1. Hi Frank,
    dein Skript ist super und ich habe das bei uns jetzt per Task eingebaut, aber bei mir funktioniert leider die Mailbenachrichtigung nicht. Hast Du mir eine Idee, woran das liegen kann? Alle 3 „Adressen“ passen, aber und er bringt mir auch keine Fehlermeldung, aber es kommt halt nichts an.
    Danke und Grüße, Heike

    Reply
  2. Pingback: Distribute databases to the corresponding servers via script ' WSUSpraxis.de
  3. Hi,

    hier fehlt zwar das Reporting welche DB nun tatsächlich verschoben wurde, aber im Grunde tut’s das hier doch auch:

    (unter $exscripts)
    .\RedistributeActiveDatabases.ps1 -DagName %DAG% -BalanceDbsByActivationPreference -ShowFinalDatabaseDistribution -Confirm:$false

    Optional läuft dazu ein Nagios-Check der den Preferred Owner checkt, wobei ich dazu übergegangen „Redistribute“ per Task einfach wiederkehrend ausführen zu lassen.

    Grüße,
    Norbert

    Reply
    • Hi Norbert,

      du hast völlig Recht. Das mitgelieferte Script macht genau was es soll. Mein Script macht es genauso und verschickt eine Mail. Im Prinzip hat man jetzt die Wahl: Silent oder mit Benachrichtigung :-)

      Danke und Gruß,
      Frank

      Reply

Leave a Comment