Thursday, May 26, 2016

Scripts for Starting and Resuming Maitenance Mode on Exchange 2013 and 2016 servers Mailbox Servers

Hi folks,

All of us are facing tasks when we need to put our Exchange server box into maintenance mode. Usually this is done when we are installing updates or need to restart the servers (in majority of cases these two walk hand by hand). In Exchange 2010 there were 2 built-in scripts for this purpose StartDagServerMaintenance.ps1 and StopDagServerMaintenance.ps1. What each of these does can be read from their names.

However both Exchange 2013 and 2016 are different kind of animal compared to Exchange 2010 and in order to place them on maintenance you will need to disable more than just DAG and Windows cluster related settings. You will also need to drain transport queue and redirect all messages to other mailbox server (yes, mailbox servers are now responsible for transport). And when server is back up and running after maintenance you will need to revert it back from maintenance. It involves a lot of  commands and some of them can be missed or omitted. You can read more about it here.

Fortunately there is a solution for Exchange techies, especially those who are as lazy as I am. There are 2 scripts that were created for start and stop of Exchange server maintenance and they are available for download at below links:
Start Exchange 2013/16 box maintenance
Stop Exchange 2013/16 maintenance

I used them in my lab where I wanted to upgrade my Exchange 2016 Preview boxes to Exchange 2016 CU1.

First I placed both of them into Exchange scripts folder that is located at the installation path and renamed files to exclude version number.


Please note that if you have restricted PowerShell execution policy in your environment you may need to temporarily disable it by running

Set-PowerShellExecutionPolicy Unrestricted

After this you can run your script as follows:

cd $exscripts
.\Start-ExchangeServerMaintenanceMode.ps1 -Server Server1.domain.com -TargetServerFQDN Server2.domain.com

Script will magically put offline all the components which should be disabled and inactivated.



After it has been executed run the following command to ensure that all the components are inactive or offline:

Get-ServerComponentState -Identity Server1.domain.com



After I saw that all the critical components were inactive I have started upgrade of my Exchange 2016 Preview installation to CU1. Of course you know the command:

Setup.exe /m:Upgrade /IAcceptExchangeServerLicenseTerms



After successful upgrade I have recovered server from the maintenance mode by running the following code:

cd $exscripts
.\Stop-ExchangeServerMaintenanceMode.ps1 -Server Server1


After server has been returned back to functionality I have checked components by running Get-ServerComponentState command again (you can also run Test-Servicehealth cmdlet, however it reports of the state only of the back end components, totally ignoring frond end ones).



Finally you need to ensure that all DBs are activated by the DB activation preference. This can be achieved by running the following code wile in the scripts folder:

.\RedistributeActiveDatabases.ps1 -DagName DAG01 -BalanceDbsByActivationPreference -Confirm:$false



Enjoy