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

HP Virtual SAN Appliance (formerly LeftHand)

Its been a while since I wrote something, so I feel obligated to make a post geek related. A few months back I started looking into a way to offer some sort of high availability to our sites while decreasing the cost. Previously we would cluster up the SQL server and hope for the best. Obviously this is not great, and still quite expensive to only have 2 servers with any level of redundancy.

When I started looking at shared storage and VMWare solutions, I came into the same issues. There are not many choices in the realm shared storage that is fast and offers redundancy without using multiple units. That is when I started looking at the VSA software. If you have not seen this before, it is pretty cool. Essentially you take the DAS disk from multiple ESX servers and create RAID sets across the servers.

This presents a few pros and cons depending on how you design this, and what you are going to use it for. If you use at least 3 servers and RAID 10 sets to provision your LUNs, then you can lose one of your 3 ESX hosts and you will stay running. With vMotion from VMWare, any servers on that host will fire back up on one of the other hosts. You need to be careful that there are enough resources to lose 1 server. Another positive to this design is that you are able to expand out by adding disk shelves, additional servers, or even moving to an HP SAN later (like a p2000). Expanding out more disk, or moving storage around is simple with the VSA interface. Very impressed so far.

There is a downside to all of this. There is a ton of disk overhead to contend with. You do have choices, but the most popular appears to be RAID 5 sets for the raw disk, and then RAID 10 on top of that. Its pretty obvious that this will slow things down a little bit when it comes to disk. You can combat this by using fast disk, but there is only so much you can do. If you are using very high I/O applications, I would not recommend this solution.

We are lucky that the applications we use for our remote sites do not have really high disk I/O. This solution allows us to offer highly available applications and makes it easy to expand as their needs change.

Enable SNMP in ESXi 4.1 or ESX 4.0

There are many reasons you will have for wanting to enable SNMP on your ESX hosts, and a whole slew of applications out there to monitor your environment. I have been playing with the Orion APM module lately, and it is not too bad. The newest version now has some support for virtual hosts, so I figured I would add those to see what kind of goodies they have. These directions may work for other versions of ESX, but these are the only 2 I currently have in my environment and I know this will work for those.

First off, you will need to download the VMware vSphere CLI. This is different from the VMware powershell installation and will allow you to make a run quite a few common commands remotely against your ESX environment from Windows or Linux systems.

Once this is installed, we need to configure your community, string and port to be used. If you are using Windows, you need to launch the CLI and change the directory to the bin folder. Here is the syntax to configure:

vicfg-snmp.pl --server  -c  -p 161 -t @161/

You will be prompted for the root username and password for the ESX host.

Next we will need to enable it:

vicfg-snmp.pl --server  -E

Again you will be prompted for the root username and password.

Now you should be able to add the host to Orion, or whatever your monitoring application of choice is.

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.