Get output added in powershell to body of email in html

Ron Shorts
Ron Shorts used Ask the Experts™
on
Hi Experts,

Looking for assistance in getting the message count number into the body of the email on the below script.

This line  {MessageCount -gt 5} I'm looking to have on a seperate line in body of the email reading as "Message Queue threshold is set at 5"

Also, looking to write another line in the body as well with just a custom message.


Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

$transportservers = Get-ExchangeServer

$a = "<style>"

$a = $a + "BODY{background-color:White;}"

$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"

$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:#00FFFF}"

$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:Paleblue}"

$a = $a + "</style>"

$all = @()

foreach ($server in $transportservers)

{

Write-host "Checking Server $server"

$Details = Get-Queue -Server $server -filter {MessageCount -gt 5} -erroraction "silentlycontinue"

if ($details)

{

$all += $details

}

}

If($all)

{

$all | ConvertTo-Html -head $a -body "<h2> Exchange 2013 Queue Alert</h2>" QueueIdentity, DeliveryType, Status, MessageCount, NextHopDomain, Lasterror | Set-Content "Queue Alert_details.html"

Send-MailMessage -From email@yourdomain.com -To email@yourdomain.com -Subject "Exchange 2013 Queue Alert" -Body (gc "Queue Alert_details.html"| out-string) -bodyAsHtml -SmtpServer smtp.yourdomain.com



}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Can't test it at the moment, but should work:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
$htmlFile = "Queue Alert_details.html"
$transportservers = Get-ExchangeServer

$head = @"
<style>
	BODY{background-color:White;}
	TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
	TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:#00FFFF}
	TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:Paleblue}
</style>
"@

$body = @"
<h2>Exchange 2013 Queue Alert</h2>
Message Queue threshold is set at 5<br />
<br />
Just a custom message<br />
"@

$all = @()

ForEach ($server in $transportservers) {
	Write-host "Checking Server $server"
	$Details = Get-Queue -Server $server -Filter {MessageCount -gt 5} -ErrorAction "SilentlyContinue"
	If ($details) {
		$all += $details
	}
}

If ($all) {
	$all |
		ConvertTo-Html -Head $head -Body $body -Property QueueIdentity, DeliveryType, Status, MessageCount, NextHopDomain, Lasterror |
		Set-Content -Path $htmlFile

	Send-MailMessage -From email@yourdomain.com -To email@yourdomain.com -Subject "Exchange 2013 Queue Alert" -Body (Get-Content -Path $htmlFile -Raw) -BodyAsHtml -SmtpServer smtp.yourdomain.com
}

Open in new window

Author

Commented:
oBdA - absolutely perfect!  Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial