Thursday, 19 August 2021

Email Queue Report

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

$Output = $null
$HtmlReport = $null
$Output = [system.collections.arraylist]@()
$HtmlReport=@()

$head = @"
<style>
h1, h5, th { text-align: center; }
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: #dae5f4; }
tr:nth-child(odd) { background: #b8d1f3; }
</style>
"@

$Server = @(
"Server001"
"Server002",
"Server003",
"Server004"
)
foreach($s in $Server)
{
    Write-Host "`n"
    Write-Host "Queue Status for Server: " -ForegroundColor Yellow -NoNewline
    Write-Host "$s" -ForegroundColor Green
    $body="<h3>[ $($s) ] Queue Report: on $(Get-Date)</h3>"
    $data= Get-Queue -Server $s |?{$_.DeliveryType -notlike "*ShadowRedundancy*" } | sort messagecount -Descending| `
Select Identity,DeliveryType,Status,MessageCount,NextHopDomain 
    Write-Host "[ Completed ]" -ForegroundColor Cyan
    $null =$output.Add($data)
    $HtmlReport += $data | ConvertTo-Html -Body $body -Head $head 
}
$Output|Sort-Object MessageCount -Descending|ft

# Details of the Login Username and SMTP server. These can very from one 
$SMTPServer = "smtprelay.office365.com"
$SMTPPort = "25"
#$Username = "sumant.prasad@test.com"
#$Password = "Password"
#Add email address of person in to & cc  whom you are sending email (Multiple addresses should be comma seperated)
$to = "Messaging@test.com,MessagingHealthReport@test.com"
#$cc = "sumant.prasad@test.com"
$subject = "All Location Email Queue Report"
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $HtmlReport
$message.to.add($to)
#$message.cc.add($cc)
$message.IsBodyHtml = $True
$message.from = "MessagingHealthReport@intertrustgroup.com"

<#If needed to send file as attachment
$attachment = "$Path\CaymanMailboxPermission.csv"
$attach_log = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attachment)
#>

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
#$smtp.EnableSSL = $true
#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
Write-Host "Email Sent"

No comments:

Post a Comment