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

Exchange 2010 – Automatically disable ActiveSync for mailboxes

For some reason Microsoft, in their infinite wisdom, decided to enable ActiveSync for all mailboxes by default. Not only did they make this the default, they did not provide any simple method for changing this feature. Luckily, there are little documented features called cmdlet extension agents that we can leverage to get around this limitation. The one we are going to use in this example is the “Scripting Agent.”

In order to use this, you will first need to enable the scripting agent by opening the Exchange Management Shell and executing this command:

Enable-CmdletExtensionAgent "Scripting Agent"

Next, you will need to create an xml file to be called every time a cmdlet is run on the server. There are a few APIs that can be used, but in our example we will be using the “OnComplete” API. You can take this text and use this to create the xml file:



    

         
              if($succeeded) {
                Set-CASMailbox $provisioningHandler.UserSpecifiedParameters["Alias"] -ActiveSyncEnabled $false
               }
         
     

In this example, whenever a new-mailbox or enable-mailbox command is run, the OnComplete API is called to run the command to disable the ActiveSync mailbox. You can easily modify this to run any other commands you would like whenever a mailbox is created or enabled. In our case, we also wanted to enable the single item recovery feature. Here is what that would look like:



    

         
              if($succeeded) {
                  Set-Mailbox $provisioningHandler.UserSpecifiedParameters["Alias"] -SingleItemRecoveryEnabled $true
                Set-CASMailbox $provisioningHandler.UserSpecifiedParameters["Alias"] -ActiveSyncEnabled $false
               }
         
     

Once you have the xml file created, you need to save it as ScriptingAgentConfig.xml. This file needs to be saved on all of your Exchange 2010 servers before it will work:

\V14\Bin\CmdletExtensionAgents\

In that path, you will also see a ScriptingAgentConfig.xml.sample. In this file, there are a few more sample ideas that might be beneficial for your environment.