Powershell 2.0 – HTML Emails

I’ve been super busy lately, and the this page is my absolute lowest priority. I looked back at some of the stuff I have done lately, and figured I might as well write a little article to show how to send HTML emails from Powershell. There is some decent information out on the internet already, but since this changed from Powershell v1.0 to v2.0, much of it is out of date. I am not going to bother with how to do this in v1.0, since most people have moved on to v2.0.

I am going to use a few basic queries that I schedule to run weekly for our junior engineers. These are just some examples to show you how it works. For many of you, this is old news.

Since I schedule these, I load the Exchange 2010 snapin at the start. This is something to keep in mind if you schedule any powershell scripts that use other snapins.


#Load the Exchange snapin for 2010
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

Next you need to format the HTML message. If you are putting together these scripts, I will assume you already have a good understanding of fonts, colors, etc. I tried to choose neutral colors for this example, keep things easy on the eyes.


$a = ""

This here is enough to make things happen, but lets say you want to add your company logo from your intranet? That’s pretty easy to, just replace the URL with the one you want to display here.


$b = " `
	

Exchange 2010 DAG
Database status

" $c = "

Exchange 2010 DAG
Database Copy Status

"

Now you need some content to send? I assume if you are reading this, you probably already have queries you need to output nice and pretty… if not, here are some for Exchange 2010.


$body = Get-MailboxDatabase -Status | sort-Object AdminDisplayName | Select-Object AdminDisplayName, `
	AvailableNewMailboxSpace, DatabaseSize, LastFullBackup, MountedOnServer | ConvertTo-Html -Head $a -Body $b 
	
$body2 = Get-MailboxDatabase | Sort-Object name | Get-MailboxDatabaseCopyStatus | Select-Object name, `
	status, contentindexstate | ConvertTo-Html -Head $a -Body $c 

Now to the part you really want to see. Here is how you can send an email, the easy way, that contains all of the above information.


Send-MailMessage -To "YourEmail@YourDomain.com" -From "Exchange@YourDomain.com" `
	-Subject "Exchange Mail Database Status" -SmtpServer "SMTP.YourDomain.com" -Body `
	($body, $body2| Out-String) -BodyAsHtml

Here is the script all put together!


#Load the Exchange snapin for 2010
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

$a = ""
$b = " `
	

Exchange 2010 DAG
Database status

" $c = "

Exchange 2010 DAG
Database Copy Status

" $body = Get-MailboxDatabase -Status | sort-Object AdminDisplayName | Select-Object AdminDisplayName, ` AvailableNewMailboxSpace, DatabaseSize, LastFullBackup, MountedOnServer | ConvertTo-Html -Head $a -Body $b $body2 = Get-MailboxDatabase | Sort-Object name | Get-MailboxDatabaseCopyStatus | Select-Object name, ` status, contentindexstate | ConvertTo-Html -Head $a -Body $c Send-MailMessage -To "YourEmail@YourDomain.com" -From "Exchange@YourDomain.com" ` -Subject "Exchange Mail Database Status" -SmtpServer "SMTP.YourDomain.com" -Body ` ($body, $body2| Out-String) -BodyAsHtml