Monitoring Exchange Services on a Remote Server using Powershell and WMI

The purpose of this script is to monitor Exchange Services from a remote server, if any of the services failed then trigger an email alert using one of the present SMTP server. This requires that the SMTP server allow for anonymous relay for the specific IP address of the server that this script runs under. This script can be triggered as a scheduled task and send an alert to an Exchange Admin warning he/she that a service is down. An example of how this would be useful:

Let’s say you have 2 edge servers (Server A and Server B ). You can use Server A to remotely monitor services on Server B and vice versa. Let’s say Server A detected an Exchange Service on Server B crashed, an alert will be sent to an Exchange Administrator so he can log in and fix the problem before Server A become too bogged down causing delays in email traffics.

This script can be edited to monitor any other remote services you deemed neccessary, don’t just limit yourself to Exchange Services.

—-Start of Script
# Getting status of Exchange Services on a remote server and look for anything that’s “stopped” using Get-WMIObject
$ServiceStatus= Get-WmiObject Win32_Service -ComputerName PN-exch05 | Where-Object {$_.name -like “MSExch*”} | Where-Object {$_.state -eq “Stopped”} | Select-Object Name, Status, State

# Convert Result to String
$ServiceStatusText = $ServiceStatus | ft | Out-String
$ServiceStatusText

# If $ServiceStatusText <> $null then send notification
If ($ServiceStatusText -ne $null)
{
###Exchange Server Values
$FromAddress = “Exchange-Alert@PNLAB.com“”
$ToAddress = “pnguyen@pnlab.com
$MessageSubject = “THIS IS A TEST, WMI Monitoring service on a remote server! CRITICAL: An exchange service is has stopped”
$MessageBody = “One or more Exchange services has stopped running or crashed. Please check the server ASAP for possible issues`n”
$MessageBody = $MessageBody + $ServiceStatusText
$SendingServer = “PN-exch02.pnlab.com”

###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress,
$MessageSubject, $MessageBody

###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)
}

# Else don’t do anything and exit
Else
{
$null
}
——-END OF SCRIPT

DISCLAIMER: I AM NOT RESPONSIBLE FOR ANY POSSIBLE ISSUE OR ISSUES THIS ARTICLE COULD POTENTIAL CAUSE TO YOUR ENVIRONMENT. THE INFORMATION PROVIDED HERE IS SIMPLY FOR MY OWN DOCUMENTATION OF THE THINGS I HAVE LEARNED

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: