Email Reporting with PowerShell

I’m a huge fan of reports when it comes to my IT infrastructure. I like being able to tangibly see numbers change over time, or even better, stay the same.

One of the ways I accomplish this is by setting up HTML reporting in almost all of my production scripts. It’s great to write a script that automatically prunes a directory or makes changes to a config that was mistakenly changed, but I want to be notified about it when it happens.

PowerShell includes theĀ -BodyAsHTML parameter with theĀ Send-MailMessage cmdlet, which allows us to easily build dynamic reports while a script is running.

But first, we need to set a variable with some necessary HTML.

$HTMLTop = "<html><head><style>table{border: 1px solid black; border-collapse: collapse;}th{border: 1px solid black; border-collapse: collapse;}td{border: 1px solid black; border-collapse: collapse;}</style></head><body>"

In $HTMLTop, we are doing a few things:

  1. We are declaring the following text to be HTML
  2. We are declaring a <head> section and defining a style for the <table> tag so that any table(s) we create will have borders and pop out in the report.
  3. We are declaring a <body> tag, so that we can start building the rest of our HTML output.

Now, to create an HTML table, we need to create a second variable:

$HTMLTable = "<p><table><th>ColumnHeader1</th><th>ColumnHeader2</th><th>ColumnHeader3</th>"

$HTMLTable defines the top row (headers) of our HTML table. Now we just need to add content. As your script processes and results are generated, you just need to add new rows to $HTMLTable.

$HTMLTable += "<tr><td>ColumnData1</td><td>ColumnData2</td><td>ColumnData3</td></tr>"

Note: This is just basic HTML, so you can add coloring to your reporting using inline CSS. For example:

<td style=color:green;font-weight:bold;>Success</td>

To close out your HTML table, append the closing tag to your existing variable:

$HTMLTable += "</table>

You can also create additional elements for your report:

$HTMLMessage = "<p>This report tracks disk usage</p>"

Now to put it all together and close out the HTML:

$HTMLFinal = $HTMLTop + $HTMLMessage + $HTMLTable + "</body></html>"

We now have a fully formed HTML report ($HTMLFinal) that we can use with the -Body parameter when we use Send-MailMessage.

Send-MailMessage -SMTPServer <your server> -To <smtp address> -From <smtp address> -Subject <your subject> -Body $HTMLFinal -BodyAsHTML

Here is an example email report from a script I wrote that reports on our VMware datastore utilization each morning:

Testing Network Port Access

While configuring IMAPS access to an Exchange 2013 server recently, I found the following utilities extremely useful while testing firewall configurations.

To test port access on a remote (or local) system, you can use PortQry (on Windows) or Netcat (on Linux/MacOS):

PortQry (Download from Microsoft)

#Syntax/Example
portqry.exe -n <sub.domain.com> -p <protocol> -e <port>
portqry.exe -n mail.domain.com -p TCP -e 993

#Successful result
cmd.exe> PortQry.exe -n mail.domain.com -p TCP -e 993
...
TCP port 993 (imaps service): LISTENING

Netcat (included with MacOS/Linux)

#Syntax/Example
nc -zv <sub.domain.com> <port>
nc -zv mail.domain.com 993

#Successful result
MAC-Terminal:~ Username$ nc -zv mail.domain.com 993
...
Connection to mail.domain.com port 993 [tcp/imaps] succeeded!

Migrating mailboxes between Exchange 2007 and Exchange 2013

Mailbox migrations between Ex2007 and Ex2013 (in either direction) have to be done via PowerShell. Use the New-MoveRequest cmdlet, and provide the Mailbox and Destination Database:

New-MoveRequest -Identity 'Mailbox1' -TargetDatabase "MailboxDatabase1

To check the progress of your migration:

Get-MoveRequest | Get-MoveRequestStatistics

Fix first paragraph font size in Ghost Blog/Casper theme posts

I originally built this blog on top of the Ghost blogging platform until switching to WordPress. After upgrading Ghost from v1.1 to v1.5, I noticed immediately that the font size of the first paragraph in all of my Ghost posts was noticeably larger than the following paragraphs.

I didn’t like this change, so dug around the code to figure out what was causing it.

This special formatting is specified in the .post-template .kg-card-markdown > p:first-child class in screen.css.

/var/www/ghost/current/content/themes/casper/assets/css/screen.css

.post-template .kg-card-markdown > p:first-child {
 font-size: 1.25em;
 line-height: 1.5em;
}

In general, it’s best practice not to edit these system files directly, as future upgrades to Ghost will overwrite any changes you made. But the good news is that Ghost provides an easy option to cleanly edit formatting with Code Injection.

To remove this special formatting and return the font size of the first paragraph of your Ghost posts back to normal, paste the following code into the Blog Header section of the Code Injection Page in your Ghost Admin site, then click Save.

Ghost Admin Portal > Code Injection > Blog Header

<!-- Fix first paragraph font size -->
<style type="text/css">
 .post-template .kg-card-markdown > p:first-child {font-size: 1em;}
</style>

Disable ServerManager on system boot

ServerManager is a useful application for installing, configuring and managing roles and features on a Windows Server machine. However, once your system is configured, it can be a nuisance by popping up when you don’t need it and eating up system memory.

Here are two scripts to disable the ServerManager application from starting up on system boot. The first disables the scheduled task that is responsible for launching the application at system boot, and the second disables the application from launching in the registry.

Disable the Scheduled Task

Disable via the registry

Updating and Recovering SuperMicro IPMI

Below are a few methods to update or recover SuperMicro IPMI on X-9 series boards. These instructions may be applicable to other board models as well. Check SuperMicro’s documentation.

This information is provided as-is, and extreme caution should always be taken when performing firmware updates.

IPMI UPGRADES

Using the IPMI interface

  1. Log into the IPMI web interface
  2. Click on Maintenance > Firmware Update. You will be prompted to enter Update Mode
    Note #1 – Do not enter Update Mode unless you are ready to update the IPMI. IPMI will reboot after leaving Update Mode.
    Note #2 – I would highly recommend that you set up a DHCP reservation for the BMC mac address. Depending on whether you choose to preserve configuration during the upgrade and how your networking is set up, the BMC IP Address may change when it boots back up. If you do not set up a reservation, the BMC device can still be found on your network within your DHCP range using IPMIView, but the specific device may be difficult to find depending on the number of SuperMicro devices in your environment.
  3. Click Enter Update Mode.
  4. Browse to and select the firmware file you want to upgrade with and follow the steps to upgrade the IPMI. You can choose to either preserve the existing configuration, or not preserve it (reset the IPMI device to factory settings).
  5. Wait for the upgrade to complete. Once the upgrade completes, click Ok to reboot the IPMI.

Using a bootable DOS flash drive and dupdate.exe

  1. Prepare a bootable DOS USB flash drive
  2. Head over to SuperMicro’s support page and download the latest IPMI for your model motherboard.
  3. The .zip that you download should contain a ‘DOS x.xx’ folder, including `dupdate.exe`. Copy the ‘DOS x.xx’ folder and the IPMI image file on to the flash drive and install it in the SuperMicro server.
  4. Reboot the server and boot into the flash drive.
  5. At the prompt, type `dupdate.exe -f <name of firmware image>`
    Note: You can optionally add `-r n` to the command above to not preserve your IPMI’s configuration (reset it to factory).
  6. Wait for the image to flash. The IPMI device will reboot automatically once it is finished.

IPMI Recovery

Using a bootable DOS flash drive and dupdate.exe

The instructions directly above can also be used to recover an IPMI device if it gets into a corrupted state.

Using SMCIPMITool

You can use SMCIPMITool to flash the firmware of an IPMI device. This is an Out of Band tool, and will require that the IPMI device have a Node Product Key Installed.

The user manual is below, which lists out the commands needed depending on which series of motherboard is installed in your SuperMicro server:

ftp://ftp.supermicro.com/utility/SMCIPMItool/SMCIPMITool_User_Guide.pdf

Updating and Recovering SuperMicro BIOS

Below are a few methods to update or recover SuperMicro BIOS on X-9 series boards. These instructions may be applicable to other board models as well. Check SuperMicro’s documentation.

This information is provided as-is, and extreme caution should always be taken when performing BIOS updates.

BIOS Upgrades

There are a few ways that you can update the BIOS in a SuperMicro server.

Using a bootable DOS flash drive and AMI.BAT

  1. Start by preparing a bootable DOS USB flash drive (or CD). There are a bunch of different tools that you can use to do this, but I like using Rufus.
  2. Next, head over to SuperMicro’s support page and download the latest BIOS for your model motherboard. Inside the .zip that is downloaded is a batch file called ami.bat.
  3. Copy the entire folder, readme’s and all, onto the flash drive and install it in the SuperMicro server.
  4. Console into the server, either directly or via IPMI, and power it on. Boot into the flash drive.
  5. Once the DOS prompt loads, type ami.bat <bios.img>, where <bios.img> is the filename of the BIOS image you downloaded.
  6. Wait for the flash to complete, and reboot when prompted. Before allowing the box to POST, power it down, remove the flash drive (or CD), remove the power cables and clear the CMOS.
  7. Replace the cabling and power the server on. The server will POST, show the SuperMicro splash screen and then quickly reboot a few times. This is normal – do not interrupt the server.

If the BIOS flashed correctly, after a few quick reboots the server will continue booting past the splash screen. Make sure you boot into Setup and change whatever BIOS settings are required for the server.

Using IPMI

In newer versions of SuperMicro’s IPMI, you can update the BIOS directly from the IPMI GUI. This requires a Supermicro OOB (Out of band) License, however. This is a per-device license, and usually retails for around $20/each.

  1. Once you have a Node Product Key for the IPMI device, log into the IPMI web interface.
  2. Click on Maintenance > BIOS update
    Note: If you don’t see BIOS update listed as an option, you may need to upgrade your firmware to a newer version, or this feature may not be supported on your motherboard.
  3. If you haven’t entered the Node Product Key into the IPMI yet, you will be required to enter it here before proceeding.
  4. Once licensed, click Browse and locate the BIOS image.
  5. Click Upload BIOS and follow the steps to complete the upgrade.

BIOS Recovery

If you get into a situation where the BIOS on your SuperMicro server is corrupted and the system will not boot properly, follow one of the sections below:

Recover BIOS via IPMI

Refer to the BIOS Upgrades > Using IPMI section directly above. Using this method, I was able to recover a SuperMicro server that refused to boot after the BIOS wound up in a corrupted state. Again, this method requires a SuperMicro OOB license (worth it).

Using a USB flash drive

Note – I haven’t successfully used this method before, but other folks have reported success with recovering their BIOS this way.

  1. Download a fresh copy of the BIOS from SuperMicro’s support site. Rename this file to SUPER.ROM (case sensitive)
  2. Copy this file to a FAT32 USB flash drive (doesn’t need to be bootable).
  3. Install the flash drive into the motherboard of the server, along with a USB keyboard.
  4. Boot the system while holding down ctrl + home, until the USB flash drive’s LED turns on. Once the LED turns on, let go of the keys.
  5. Wait for the BIOS to flash, after which the server with reboot automatically.