check (#exchange) message queue size and send email alert

$snapins = Get-PSSnapin
$snapins | foreach-Object {

if ($_.name -match “Exchange”)
{
$exchloaded = $TRUE
}
}
if ($exchloaded -eq $TRUE)
{
if ($showgui)
{
Write-Host -ForegroundColor Green “Exchange 2010 Snapin already loaded.”
}
}
else
{
Add-PSSnapin *Exchange*
if ( $showgui ) { Write-Host -ForegroundColor Green “Exchange 2010 Snapin had to be loaded.” }
}

$filename = “C:\temp\queue.txt”
Start-Sleep -s 10
if (Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true } | get-queue | Where-Object { $_.MessageCount -gt 1000 })

{

Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true } | get-queue | Where-Object { $_.MessageCount -gt 1000 } | Format-Table -Wrap -AutoSize | out-file -filepath C:\temp\queue.txt
Start-Sleep -s 10

$smtpServer = “x.x.x.x” #your smtp server
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = “[email protected]” #send as address
$msg.To.Add(“[email protected]”) #change this address for admin address
$msg.Subject = “CAS SERVER QUEUE THRESHOLD REACHED – PLEASE CHECK EXCHANGE QUEUES”
$msg.Body = “Please see attached queue log file for queue information”
$msg.Attachments.Add($att)
$smtp.Send($msg)