Most enterprise environments have solutions that provide monitoring services to ensure Microsoft Exchange Server services are running and if they are not, restart the service and send an email notification to administrators but I have constantly come across smaller business that may not be able to afford such applications and therefore have implemented scripts with task manager scheduled tasks to provide some form of service monitoring. The Exchange Health Check Report by Paul Cunningham (https://practical365.com/exchange-server/powershell-script-exchange-server-health-check-report/) is great for daily reports but it is not practical to constantly use it for monitoring so what I’ve typically used is implement the service_check.ps1 script written by Kevin Olson:
Check for hung or stopped services
https://gallery.technet.microsoft.com/scriptcenter/Check-for-hung-or-stopped-67bc718d
The small adjustment I’ve made to the script is to move the Send-Mailmessage cmdlet to execute after the Start-Service because if you do not have another SMTP relay setup and need to rely on the Exchange server this script is monitoring, the email will not be sent out if the service that is hung or stopped is the transport service. The following are the scripts along with the Exchange services added into the script for each version:
Microsoft Exchange 2010
#NAME: service_check.ps1
#AUTHOR: Kevin Olson
#DATE: 4/29/2011
#Machine to be monitored
$Computer = "brcl-exchange"
#Create an array of all services running
$GetService = get-service -ComputerName $Computer
#Create a subset of the previous array for services you want to monitor
$ServiceArray = "MSExchangeADTopology","MSExchangeAB","MSExchangeAntispamUpdate","MSExchangeEdgeSync","MSExchangeFDS","MSExchangeIS","MSExchangeMailSubmission","MSExchangeMailboxAssistants","MSExchangeMailboxReplication","MSExchangeProtectedServiceHost","MSExchangeRepl","MSExchangeRPC","MSExchangeSearch","MSExchangeServiceHost","MSExchangeSA","MSExchangeThrottling","MSExchangeTransport","MSExchangeTransportLogSearch","MSExchangeFBA","W3SVC";
#Find any iWFM service that is stopped
foreach ($Service in $GetService)
{
foreach ($srv in $ServiceArray)
{
if ($Service.name -eq $srv)
{
#check if a service is hung
if ($Service.status -eq "StopPending")
{
$servicePID = (gwmi win32_Service | where { $_.Name -eq $srv}).ProcessID
Stop-Process $ServicePID
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv)
#email to notify if a service is down
Send-Mailmessage -to administrator@someDomain.com -Subject "$srv is hung on $Computer" -from exchange@contoso.com -Body "The $srv service was found hung." -SmtpServer localhost
}
# check if a service is stopped
elseif ($Service.status -eq "Stopped")
{
#automatically restart the service.
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv)
#email to notify if a service is down
Send-Mailmessage -to administrator@someDomain.com -Subject "$srv is stopped on $Computer" -from exchange@contoso.com -Body "The $srv service was found stopped." -SmtpServer localhost
}
}
}
}
The services I included in the script are all of the ones listed as Automatic as well as Automatic (Delayed Start):
Microsoft Exchange 2013
#NAME: service_check.ps1
#AUTHOR: Kevin Olson
#DATE: 4/29/2011
#Machine to be monitored
$Computer = "bm1-azim-40-001"
#Create an array of all services running
$GetService = get-service -ComputerName $Computer
#Create a subset of the previous array for services you want to monitor
$ServiceArray = “HostControllerService","MSExchangeADTopology","MSExchangeAntispamUpdate","MSExchangeDagMgmt","MSExchangeDelivery","MSExchangeDiagnostics","MSExchangeEdgeSync","MSExchangeFastSearch","MSExchangeFrontEndTransport","MSExchangeHM","MSExchangeIS","MSExchangeMailboxAssistants","MSExchangeMailboxReplication","MSExchangeRepl","MSExchangeRPC","MSExchangeServiceHost","MSExchangeSubmission","MSExchangeThrottling","MSExchangeTransport","MSExchangeTransportLogSearch","MSExchangeUM","MSExchangeUMCR","W3SVC";
#Find any iWFM service that is stopped
foreach ($Service in $GetService)
{
foreach ($srv in $ServiceArray)
{
if ($Service.name -eq $srv)
{
#check if a service is hung
if ($Service.status -eq "StopPending")
{
$servicePID = (gwmi win32_Service | where { $_.Name -eq $srv}).ProcessID
Stop-Process $ServicePID
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv)
#email to notify if a service is down
Send-Mailmessage -to administrator@someDomain.com -Subject "$srv is hung on $Computer" -from exchange@contoso.com -Body "The $srv service was found hung." -SmtpServer localhost
}
# check if a service is stopped
elseif ($Service.status -eq "Stopped")
{
#automatically restart the service.
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv)
#email to notify if a service is down
Send-Mailmessage -to administrator@someDomain.com -Subject "$srv is stopped on $Computer" -from exchange@contoso.com -Body "The $srv service was found stopped." -SmtpServer localhost
}
}
}
}
The services I included in the script are all of the ones listed as Automatic as well as Automatic (Delayed Start):
World Wide Web Publish Service is also included.
Microsoft Exchange 2016
#NAME: service_check.ps1
#AUTHOR: Kevin Olson
#DATE: 4/29/2011
#Machine to be monitored
$Computer = "prpmbx16-02"
#Create an array of all services running
$GetService = get-service -ComputerName $Computer
#Create a subset of the previous array for services you want to monitor
$ServiceArray = "HostControllerService","MSComplianceAudit","MSExchangeADTopology","MSExchangeAntispamUpdate","MSExchangeCompliance","MSExchangeDagMgmt","MSExchangeDelivery","MSExchangeDiagnostics","MSExchangeEdgeSync","MSExchangeFastSearch","MSExchangeFrontEndTransport","MSExchangeHM","MSExchangeHMRecovery","MSExchangeIS","MSExchangeMailboxAssistants","MSExchangeMailboxReplication","MSExchangeRepl","MSExchangeRPC","MSExchangeServiceHost","MSExchangeSubmission","MSExchangeThrottling","MSExchangeTransport","MSExchangeTransportLogSearch","MSExchangeUM","MSExchangeUMCR","W3SVC";
#Find any iWFM service that is stopped
foreach ($Service in $GetService)
{
foreach ($srv in $ServiceArray)
{
if ($Service.name -eq $srv)
{
#check if a service is hung
if ($Service.status -eq "StopPending")
{
$servicePID = (gwmi win32_Service | where { $_.Name -eq $srv}).ProcessID
Stop-Process $ServicePID
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv)
#email to notify if a service is down
Send-Mailmessage -to administrator@someDomain.com -Subject "$srv is hung on $Computer" -from exchange@contoso.com -Body "The $srv service was found hung." -SmtpServer localhost
}
# check if a service is stopped
elseif ($Service.status -eq "Stopped")
{
#automatically restart the service.
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv)
#email to notify if a service is down
Send-Mailmessage -to administrator@someDomain.com -Subject "$srv is stopped on $Computer" -from exchange@contoso.com -Body "The $srv service was found stopped." -SmtpServer localhost
}
}
}
}
The services I included in the script are all of the ones listed as Automatic (Microsoft Exchange Notifications Broker is excluded) as well as Automatic (Delayed Start):
World Wide Web Publish Service is also included.
Task Scheduler Configuration
One of the methods to execute the script repeatedly on the server is to create a task in the Task Scheduler as such:
Create a trigger and specify a Repeat task every however frequently you like:
Then create an action with powershell.exe as the Program/script and the following as the Add arguments (optional) field:
-command "& 'C:\Scripts\service_check.ps1'"
Extra Setup Information
This script can be used for other services as well and an easy way of obtaining the service names to monitor is use cmdlets such as the one below to list all the services that contains, say, Microsoft Exchange:
Get-Service | Where {$_.DisplayName -like "Microsoft Exchange*"} | format-table -autosize
Status Name DisplayName
------ ---- -----------
Running HostControllerService Microsoft Exchange Search Host Controller
Running MSComplianceAudit Microsoft Exchange Compliance Audit
Running MSExchangeADTopology Microsoft Exchange Active Directory Topology
Running MSExchangeAntispamUpdate Microsoft Exchange Anti-spam Update
Running MSExchangeCompliance Microsoft Exchange Compliance Service
Running MSExchangeDagMgmt Microsoft Exchange DAG Management
Running MSExchangeDelivery Microsoft Exchange Mailbox Transport Delivery
Running MSExchangeDiagnostics Microsoft Exchange Diagnostics
Running MSExchangeEdgeSync Microsoft Exchange EdgeSync
Running MSExchangeFastSearch Microsoft Exchange Search
Running MSExchangeFrontEndTransport Microsoft Exchange Frontend Transport
Running MSExchangeHM Microsoft Exchange Health Manager
Running MSExchangeHMRecovery Microsoft Exchange Health Manager Recovery
Stopped MSExchangeImap4 Microsoft Exchange IMAP4
Stopped MSExchangeIMAP4BE Microsoft Exchange IMAP4 Backend
Running MSExchangeIS Microsoft Exchange Information Store
Running MSExchangeMailboxAssistants Microsoft Exchange Mailbox Assistants
Running MSExchangeMailboxReplication Microsoft Exchange Mailbox Replication
Stopped MSExchangeNotificationsBroker Microsoft Exchange Notifications Broker
Stopped MSExchangePop3 Microsoft Exchange POP3
Stopped MSExchangePOP3BE Microsoft Exchange POP3 Backend
Running MSExchangeRepl Microsoft Exchange Replication
Running MSExchangeRPC Microsoft Exchange RPC Client Access
Running MSExchangeServiceHost Microsoft Exchange Service Host
Running MSExchangeSubmission Microsoft Exchange Mailbox Transport Submission
Running MSExchangeThrottling Microsoft Exchange Throttling
Running MSExchangeTransport Microsoft Exchange Transport
Running MSExchangeTransportLogSearch Microsoft Exchange Transport Log Search
Running MSExchangeUM Microsoft Exchange Unified Messaging
Running MSExchangeUMCR Microsoft Exchange Unified Messaging Call Router
Stopped wsbexchange Microsoft Exchange Server Extension for Windows Server Backup
Copy the output to a text file and extract the services as such:
HostControllerService
MSComplianceAudit
MSExchangeADTopology
MSExchangeAntispamUpdate
MSExchangeCompliance
MSExchangeDagMgmt
MSExchangeDelivery
MSExchangeDiagnostics
MSExchangeEdgeSync
MSExchangeFastSearch
MSExchangeFrontEndTransport
MSExchangeHM
MSExchangeHMRecovery
MSExchangeIS
MSExchangeMailboxAssistants
MSExchangeMailboxReplication
MSExchangeRepl
MSExchangeRPC
MSExchangeServiceHost
MSExchangeSubmission
MSExchangeThrottling
MSExchangeTransport
MSExchangeTransportLogSearch
MSExchangeUM
MSExchangeUMCR
1 comment:
checking on service statuses is not perfectly a reliable way. Best is use powershell to check on the status of a web page in this case OWA and if the script is getting 404 error code then it restarts the services if not then it does not. or check on 200 which means the owa page is up and user is able to login to their mailbox
Post a Comment