Exchange 2010 Datacenter failover and switchback scripts

This article has been ported from my old TechBlog as it is still valid and useful.

I’ve been lapping up some sunshine in the garden but the weather is now changing again, so it’s back indoors and time to post something. From some recent work I was doing, here are some scripts for DAG failover to DR site and switchback again. There are specific assumptions here; firstly, I was working with two mailbox servers in my main site and a single mailbox server in the DR site. Secondly, I had a single DAG. Thirdly, I was using the same CAS Array for both sites – that is not necessarily always going to be the case, but this was just a DR site, so was appropriate for me. Finally, I was blocking database activation in the DR site. You can easily modify the scripts according to your circumstances and need to modify some of the tags anyway.

< MAIN_SITE>
< DR_SITE>
< DAGNAME>
< DR_MBX_SERVER>
< CASARRAY>

In my example there are 15 mailbox databases named DB01-DB15.

#### TERMINATE FAILED DATACENTER
#### if mailbox server(s) in are still powered on but not functioning properly

POWER OFF

### or
Stop-DatabaseAvailabilityGroup -ActiveDirectorySite -Confirm:$false

#### if AD is available in but servers went off before doing the previous step
Stop-DatabaseAvailabilityGroup -ActiveDirectorySite -ConfigurationOnly -Confirm:$False

#### check that the mailbox servers have stopped in
Get-DatabaseAvailabilityGroup | select Name,*server*

#### run in
Stop-DatabaseavailabilityGroup -ActiveDirectorySite -ConfigurationOnly -Confirm:$False

#### ACTIVATE DR SITE
#### run in

stop-service ClusSvc
Restore-DatabaseAvailabilityGroup -ActiveDirectorySite -Confirm:$False

(repeat above if necessary)

1..9 | ForEach-Object {Move-ActiveMailboxDatabase DB0"$_" -ActivateOnServer -SkipActiveCopyChecks -MountDialOverride:None –confirm:$false}; 0..5 | ForEach-Object {Move-ActiveMailboxDatabase DB1"$_" -ActivateOnServer -SkipActiveCopyChecks -MountDialOverride:None –confirm:$false}
Set-clientaccessarray –site –confirm:$false


#### SWITCHBACK
#### run in after site is brought back up

Start-DatabaseAvailabilityGroup -ActiveDirectorySite
(repeat above if necessary)

Set-clientaccessarray –site –confirm:$false

#### check that mailbox servers in show as started in DAG ()

Get-DatabaseAvailabilityGroup | select Name,*server*

#### move databases back to

origlocation$=get-location; set-location exscripts$; .\RedistributeActiveDatabases.ps1 - -BalanceDbsByActivationPreference -ShowFinalDatabaseDistribution -Confirm:$false; set-location origlocation$

(repeat above if necessary)
#### if appropriate, block from activating databases when is available

Set-MailboxServer -DatabaseCopyAutoActivation:blocked