Exchange Powershell Exchange and Powershell Helps, Tips, and Tricks

Script – Check Message Queue

06.03.2010 · Posted in scripts

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.

— Start of Script —
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
— End of Script —

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.

[PS] 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.

2 Responses to “Script – Check Message Queue”

  1. Anand kumar Deva says:

    Thanks. Good job…

  2. I found that the command above would count active queues even if the message count in them is 0, resulting in an inaccurate queue. I modified the command a bit to only count queues that are greater than 0.

    Get-ExchangeServer | where {$_.isHubTransportServer -eq $true} | get-queue | where {$_.MessageCount -gt 0} |measure-object MessageCount -max

Leave a Reply