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

 del.icio.us  Stumbleupon  Technorati  Digg 

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this entry.
Comments
  • No comments exist for this entry.
Leave a comment

Submitted comments will be subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.