Tuesday, October 9, 2018

powershell: check service status and health

Had a unique situation recently with a client whose IMAP Proxy would go offline after a reboot. All the services were running, and Event Logs were less than helpful to determine root cause. So while I wait for Microsoft support to check into and resolve the root cause (haha), I wrote a basic script to to resolve preventable outages due to this bug.

The script is written to import the Exchange Management Module.

Then check the health and status of the IMAP Proxy.

If the health and status of the IMAP Proxy is NOT online and healthy, it triggers a condition to start the service.

If the health and status of the IMAP Proxy IS online and healthy, it exits the script.

This script is scheduled via Task Manager to run on startup, after a 15 minute delay (to allow normal Exchange services a chance to start).


<#
.SYNOPSIS
    Checks the health & status of the IMAP Proxy service, and starts it if not healthy and online.

.NOTES
    CREATE DATE:    2018-10-09
    CREATE AUTHOR:  Sam Dye
    REV NOTES:
            v1.0 - Completed script and deployed via Task Schedulder to run on startup, after a 15 minute delay.
       
#>

# add exchange management module
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

# check status, and start if NOT already online
Get-HealthReport -Identity "$servername" | where {$_.HealthSet -eq "IMAP.Proxy"}
    if ({$_.State -ne "Online"}) {
            Set-ServerComponentState -Identity "$servername" -Component "imapproxy" -Requester "HealthAPI" -State "Active"
        }
        elseif ({$_.State -eq "Online"}) {
            exit
}
link to code on github

No comments:

Post a Comment