Script – Check Message Queue

note: following the transfer of this domain to the new owners, per user requests this article was recovered from the internet archive wayback machine, but may not be complete.

Here is a simple script I wrote that checks the message queue and then sends an email if the message queue goes over a specified limit.

function check_queue
 {
 $a = get-transportserver | get-queue | measure-object MessageCount -max

if ($a.Maximum -gt 25)
 {
 send_email $a.Maximum
 Exit
 }
 start-sleep -s 60
 check_queue
 }

function send_email
 {param ($queue_size)

$emailFrom = "[REMOVED]"
 $emailTo = "[REMOVED]"
 $subject = "QUEUE SIZE HIGH"
 $body = "Message Queues are high, max value $queue_size"
 $smtpServer = "[REMOVED]"
 $smtp = new-object Net.Mail.SmtpClient($smtpServer)
 $smtp.Send($emailFrom, $emailTo, $subject, $body)

}

check_queue

In this script, replace the [REMOVED] with the data that is needed.  You can then run this script as a scheduled task in a PowerShell window.

All you have to do is use the start-job command.

 D:\_ps-scripts>start-job ./queue_check.ps1
WARNING: column “Command” does not fit into the display and was removed
Id              Name            State      HasMoreData     Location
–              —-            —–      ———–     ——–
1               Job1            Running    True            localhost 

And to remove the job, you just use the stop-job and remove-job command.