Failover Script for 2 server mailbox cluster

Posted: February 28, 2011 in Exchange, Scripts

A little script to failover mailbox between servers or balance the load across both of them. Replace server names as needed.

$ServerA=”ExchSrv1″
$ServerB=”ExchSrv2″

$BalanceSwitch=0
$AllMDBs=Get-MailboxDatabase

Clear

Function SwitchToASide
{
Write-Host “Failing over to A-Side…”
Get-MailboxDatabase | where {$_.Server -eq $ServerB} | Move-ActiveMailboxDatabase -ActivateOnServer $ServerA -Confirm:$false
}

Function SwitchToBSide
{
Write-Host “Failing over to B-Side…”
Get-MailboxDatabase | where {$_.Server -eq $ServerB} | Move-ActiveMailboxDatabase -ActivateOnServer $ServerA -Confirm:$false
}

Function Balance
{
Write-Host “Balancing DB load…”
foreach ($Database in $AllMDBs){
switch ($BalanceSwitch){
0 {
$CurrentDB = Get-MailboxDatabase | Where {$_.Name -eq $Database}
if ($CurrentDB.Server -eq $ServerB){
Write-Host “Migrating $CurrentDB to A-Side…”
Move-ActiveMailboxDatabase -Identity $CurrentDB.Name -ActivateOnServer $ServerA -Confirm:$false
}
$BalanceSwitch=1
}
1 {
$CurrentDB = Get-MailboxDatabase | Where {$_.Name -eq $Database}
if ($CurrentDB.Server -eq $ServerA){
Write-Host “Migrating $CurrentDB to B-Side…”
Move-ActiveMailboxDatabase -Identity $CurrentDB.Name -ActivateOnServer $ServerB -Confirm:$false
}
$BalanceSwitch=0
}
}
}
}

$Selection=Read-Host “ServerA, ServerB, Balance”

switch ($Selection)
{
ServerA {SwitchToServerA}
ServerB {SwitchToServerB}
Balance {Balance}
default {“Invalid option specified!”}

}

Get-MailboxDatabase

Advertisements

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