function check_queue
{
$QueueCount = get-transportserver | get-queue | measure-object MessageCount -max
# Set the limit here
if ($QueueCount.Maximum -gt 20)
{
send_email $QueueCount.Maximum
exit
}
# How often to check
start-sleep -s 300
check_queue
}
function send_email
{param ($queue_size)
$emailFrom = "from@who.com"
$emailTo = "to@who.com"
$subject = "Exchange queue at $queue_size"
$body = "Mail queues are high, last seen at $queue_size"
$smtpServer = "smtp.mydomain.net"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
check_queue
Use the start-job command.
start-job ./CheckQueue.ps1