skip to Main Content

Put Exchange Server in maintenance mode

Before updating the Exchange Server, it is recommended to put the Exchange Server in maintenance mode. We can put the Exchange Server in maintenance mode with some PowerShell commands. In this article, we have two Exchange Servers. The names of the Exchange Servers are EX01 and EX02. We will place Exchange Server EX01 in maintenance mode.

Put Exchange Server in maintenance mode

Sign in to Exchange Server EX01. Run Exchange Management Shell as administrator. Set the Hub Transport Service to draining. It will stop accepting any more messages.

[PS] C:\>Set-ServerComponentState -Identity "EX01" -Component HubTransport -State Draining -Requester Maintenance

Redirect any queued messages to EX02. The target Server value has to be the target server’s FQDN. The target server shouldn’t be in maintenance mode.

[PS] C:\>Redirect-Message -Server "EX01" -Target "EX02.alitajran.local"

Confirm
Are you sure you want to perform this action?
Redirecting messages to "EX02.alitajran.local".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "Y"): Y

If the server is a DAG member, run the following commands. If your server is not a DAG member, skip to the command for setting ServerWideOffline.

Pause the cluster node. Suspend Server EX01 from the DAG.

[PS] C:\>Suspend-ClusterNode "EX01"

Name       ID    State
----       --    -----
EX01       1     Paused

Disable database copy automatic activation. This command will also move any active database copies to other DAG members. Assuming there are other healthy DAG members available. This is not instantaneous, and it can take several minutes for the moves to occur. We’ll check it in one of the following commands.

[PS] C:\>Set-MailboxServer "EX01" -DatabaseCopyActivationDisabledAndMoveNow $true

Make a note of the database copy automatic activation policy on the server. You can set it back to this value at the end of maintenance. The default setting is Unrestricted.

[PS] C:\>Get-MailboxServer "EX01" | Select DatabaseCopyAutoActivationPolicy

DatabaseCopyAutoActivationPolicy
--------------------------------
                    Unrestricted

Set it to Blocked to prevent any of the databases from becoming Active.

[PS] C:\>Set-MailboxServer "EX01" -DatabaseCopyAutoActivationPolicy Blocked

Check for any database copies that are still mounted on the server. It may take a while for the Active databases to move. This command should return no results. If any database copies are still active on the server and other DAG members host copies of the database, perform a manual switchover.

[PS] C:\>Get-MailboxDatabaseCopyStatus -Server "EX01" | Where {$_.Status -eq "Mounted"}

Once the active databases have been moved, we will check the transport queue. Queues should be empty or almost empty, as we will be disabling all server components. Any emails still pending in the queues will have a delay in delivery until the server is taken out from maintenance mode.

[PS] C:\>Get-Queue

Identity        DeliveryType               Status MessageCount Velocity RiskLevel OutboundIPPool NextHopDomain
--------        ------------               ------ ------------ -------- --------- -------------- -------------
EX01\4          SmtpDeliveryToMailbox      Ready  0            0        Normal    0              db2
EX01\8          SmtpDeliveryToMailbox      Ready  0            0        Normal    0              db4
EX01\15         SmartHostConnectorDelivery Ready  0            0        Normal    0              mx1.spambull.com
EX01\20         SmtpDeliveryToMailbox      Ready  0            0        Normal    0              db5
EX01\22         SmartHostConnectorDelivery Ready  0            0        Normal    0              mx2.spambull.com
EX01\Submission Undefined                  Ready  0            0        Normal    0              Submission
EX01\Shadow\3   ShadowRedundancy           Ready  0            0        Normal    0              ex02.alitajran.local

Put the Server EX01 into maintenance mode.

[PS] C:\>Set-ServerComponentState "EX01" -Component ServerWideOffline -State Inactive -Requester Maintenance

Check the load balancer

Do you have the Exchange Server configured in a load balancer? Verify that the load balancer health checks have taken the server out of the pool or marked it as offline/inactive. If the load balancer does not automatically do this, manually mark the server as offline/inactive. Sign in to your load balancer and set any virtual services you have to disable any connections to Server EX01. Typically there would be SMTP and HTTPS virtual services. This will force any future connections to Server EX01.

How to verify Exchange Server is in maintenance mode

Verify if the Exchange Server EX01 has been placed into maintenance mode. All components should show Inactive except for Monitoring and RecoveryActionsEnabled.

[PS] C:\>Get-ServerComponentState "EX01" | Select Component, State

Component                     State
---------                     -----
ServerWideOffline          Inactive
HubTransport               Inactive
FrontendTransport          Inactive
Monitoring                   Active
RecoveryActionsEnabled       Active
AutoDiscoverProxy          Inactive
ActiveSyncProxy            Inactive
EcpProxy                   Inactive
EwsProxy                   Inactive
ImapProxy                  Inactive
OabProxy                   Inactive
OwaProxy                   Inactive
PopProxy                   Inactive
PushNotificationsProxy     Inactive
RpsProxy                   Inactive
RwsProxy                   Inactive
RpcProxy                   Inactive
UMCallRouter               Inactive
XropProxy                  Inactive
HttpProxyAvailabilityGroup Inactive
ForwardSyncDaemon          Inactive
ProvisioningRps            Inactive
MapiProxy                  Inactive
EdgeTransport              Inactive
HighAvailability           Inactive
SharedCache                Inactive
MailboxDeliveryProxy       Inactive
RoutingUpdates             Inactive
RestProxy                  Inactive
DefaultProxy               Inactive
Lsass                      Inactive
RoutingService             Inactive
E4EProxy                   Inactive
CafeLAMv2                  Inactive
LogExportProvider          Inactive

Before starting with the updates, I recommend rebooting the Exchange Server first. This will free up resources and clear any pending updates. After the reboot, you can start installing the updates.

Read more: Take Exchange Server out of maintenance mode »

Conclusion

In this article, you learned how to place the Exchange Server in maintenance mode with PowerShell. You can perform upgrades and updates during business hours without disrupting any clients. It’s good to have a list of PowerShell commands before starting.

Did you enjoy this article? You may like Get mailbox database size and white space. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 18 Comments

  1. Now aside reading and following Paul Cunningham and other Exchange MVP and MCM blogs, your site currently is one under my radar. As junior engineer, i am still astonished on your detailed and meticulous articles, your methodology is on-par with MOC’s hands-on-labs, even a dummy can follow *LOL*.
    I hope you can also cover other MS related technologies such as Hyper-V, AD and HADR-related topics.
    God bless you man, your support for community shall be rewarded. I believe sooner or later you’ll be granted MVP badge, no doubt about that..aameen! 🙂

  2. Hi Ali,

    We have 2 Exchange Servers with DAG. I would like to deploy this April 2021 security patch. Do I need to bring the exchange server to maintenance mode, or can i just run the patch manually under administrator rights?

  3. Just went over your steps and it works like a charm. Thank you for the post, very helpful!

  4. I have to
    Make upgrade tomorrow i have two mailbox servers with DAG but i dont want to run the dag commands is this possible?
    My server is exchange 2016 and the update is CU 19

      1. To be honest it is a big client and i never tried that before and it is ok with him to shut down the servers so i ll shut a server and start updating the other one

  5. Hi ALI TAJRAN, thank you!!!!
    Recently, your site comes up whenever I need accurate and critical Exchange / Powershell info, especially with DAGs. And today, I needed a refresher for the maintenance mode topic, which I had to do live in front of a customer in a live prod environment while not appearing nervous — it all went well, so thank you for spending the time to document each step with explanations. Saved my weekend.

  6. Hi,

    If I have single on-prem exchange server, do I need to put it in maintenance mode, for example, when updating CUs or security patches or Monthly Tuesday patches?

    Thanks a lot

  7. I understand redirection of the email queues, but under “C:\Program Files\Microsoft\Exchange Server\V15\scripts\StartDagServerMaintenance.ps1” is the script to do the rest.

    here is my script, let me know of any suggestions….

    # if you are using your powershell ISE console on your workstation or server. Use only on $Session.
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://hir-exmb-01.yourdomain.com/PowerShell/ -Authentication Kerberos

    # Import $Session above; only one session can be imported. If errors, close PowerShell ISE and start over
    Import-PSSession $Session | Out-File -FilePath c:\temp\can-exmb-02.txt

    # before running the StartMaintenanceMode script
    Set-ServerComponentState can-exmb-02 -Component HubTransport –State Draining –Requester Maintenance | Out-File -FilePath c:\temp\can-exmb-02.txt -Append
    Start-Sleep -Seconds 10

    # restart transport service for each server
    Get-Service -ComputerName can-exmb-02 MSExchangeTransport | Restart-Service | Out-File -FilePath c:\temp\can-exmb-02.txt -Append
    Start-Sleep -Seconds 10

    # check transport service
    Get-Service -ComputerName can-exmb-02 MSExchangeTransport | Out-File -FilePath c:\temp\can-exmb-02.txt -Append
    Start-Sleep -Seconds 10

    # Redirect message queue (message queue is moved to another server)
    Redirect-Message -Server can-exmb-02 -Target can-exmb-03.yourdomain.com | Out-File -FilePath c:\temp\can-exmb-02.txt -Append
    Start-Sleep -Seconds 10

    # Set component state
    Set-ServerComponentState can-exmb-02 -Component ServerWideOffline –State Inactive –Requester Maintenance | Out-File -FilePath c:\temp\can-exmb-02.txt -Append
    Start-Sleep -Seconds 10

    # check component state
    Get-ServerComponentState can-exmb-02 | ft Component,State -AutoSize | Out-File -FilePath c:\temp\can-exmb-02.txt -Append
    Start-Sleep -Seconds 15

    #check queues
    Get-Queue -Server can-exmb-02 | Out-File -FilePath c:\temp\can-exmb-02.txt -Append
    Start-Sleep -Seconds 10

    Move-ADObject ‘CN=can-EXMB-02,OU=Hiram Servers,OU=Servers,DC=yourdomain,DC=com’ -TargetPath ‘CN=computers,DC=yourdomain,DC=com’ | Out-File -FilePath c:\temp\can-exmb-02.txt -Append
    Start-Sleep -Seconds 10

    #change directory path
    $exscripts = “C:\Program Files\Microsoft\Exchange Server\V15\scripts”
    Set-Location $exscripts
    .\StartDagServerMaintenance.ps1 -serverName can-exmb-02 | Out-File -FilePath c:\temp\can-exmb-02.txt -Append

    #check servers in maintenace mode
    Get-databaseavailabilitygroup -status | fl name,ServersInMaintenance

Leave a Reply

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