Sunday, June 5, 2011

Monitoring Microsoft SMTP gateways queues with PowerShell

Jeffrey Snover; this is the guy behind the most amazing thing that happened to Microsoft in the past five years “my opinion” which is PowerShell;

I know that the word shell scares some people but this is different; Snover have created an interactive, programmatic and production oriented shell and overall it is a lot of fun J

He is a true example of the passionate individual who can shift the technology trend;
Enough about Sonver and let’s talk about his baby "PowerShell";

Once you get familiar with PowerShell you will enjoy it a lot as it will save you ton of time and increase your productivity, and here is one example assuming that you don’t have SCOM Server implemented already “I totally encourage you to use SCOM” and you would like to have some kind of monitoring on your Exchange Edge Servers important parts for instance the queue;

You can create a scheduled task to run a script which will monitor the number of items and if it exceeds a defined threshold you will be notified through email;

The logic will be as follow:

  • Scheduled task will be created on the edge servers that will run every 30 minutes,
  • The task will fire a batch file,
  • The batch file by its turn will launch a PowerShell script.
  • The script will import the exchange 2010 PSSnapin and will do its magic.

Batch File
Powershell -command "& {C:\Data\Scripts\Exch2010QueueMonitor.ps1}"

PowerShell File
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

$filename = “c:\Data\Scripts\Server Name.txt”
Start-Sleep -s 10
if (Get-ExchangeServer -Identity Server Name | get-queue | Where-Object { $_.MessageCount -gt 10 })


Get-ExchangeServer -Identity Server Name | get-queue |sort messagecount -descending | Where-Object { $_.MessageCount -gt 1 } | Format-Table -Wrap -AutoSize | out-file -filepath C:\Data\Scripts\Server Name.txt
Start-Sleep -s 10

$smtpServer = “SMTP Server Name or IP address
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = “
$msg.To.Add("My Email Address")
$msg.Cc.Add("Secondary Email Address")
$msg.Subject = “Server Name Server Queue Threshold Reached”
$msg.Body = “Server Name Current Queue Status”

Windows PowerShell Community Resources

By the way today is my daughter first birthday; happy birthday Mariam :)

See you soon, Hany Donia