One of PowerShell’s major strong points is its ability to interface with a variety of technologies and data sources. This makes it a great candidate for sending ad hoc notifications or generating HTML based reports. There are many ways you can do this. I added several functions to the TechNet Script Gallery leveraging methods from Don Jones’ HTML Reports in PowerShell.
A real world example
Systems Center Operations Manager (SCOM) 2012 is Microsoft’s take on monitoring the health, performance, and availability of the technical services that IT provides, and the individual components behind them. It provides fantastic flexibility, functionality, and extensibility. It does not provide shiny e-mail notifications. You get plain text, limited details on the alarm, and Microsoft’s description of the issue, which isn’t always readable by your audience:
So! We can spice things up. SCOM lets us define something called a command notification channel, that runs a command rather than sending an e-mail. We can use this to run a PowerShell script that pulls in extra alert details, including SCOM info associated with the rule or monitor (e.g. knowledge base), information from other sources (e.g. config / inventory management systems, the server in question), and presents this information in a less bland notification e-mail. I use the functions in this script to create e-mails like the following:
Inside this script and several others, I use the following functions.
ConvertTo-PropertyValue
Want to show a set of details for one object? You end up with a single difficult to read row; the more properties you want to show, the worse it gets. ConvertTo-PropertyValue will take that one object, extract all or specified properties, and create an array of property value objects. A quick example:
You can pick specific properties, member types, and headers for the property and value columns. The property order is maintained, which can be important for notifications and reports.
New-HTMLHead
This function starts building an HTML page. It opens the necessary tags, adds a default CSS internal style sheet, and leaves the body open for you to work with. You can specify an external CSS sheet and it will pull in the definition.
New-HTMLTable
The brunt of the code is in here. We take in the objects, parse them with the XDocument class, add even and odd class attributes to give rows alternating colors, and spit out the table as an HTML fragment.
For the New-HTMLTable and Add-HTMLTableColor functions, I use the XDocument class from the System.XML.Linq assembly. Don Jones uses a simpler Set-AlternatingCSSClasses based on PowerShell’s XML type accelerator.
Add-HTMLTableColor
Sometimes you need to draw attention to a value or a row. With this function, you pick a column, set up logic to deduce which cells or rows to act on, specify the CSS declaration to add inline (color in this example), and you get back the modified HTML.
Taking the event table from the last example, we can add color to highlight warning and error events:
This function takes in the HTML table string and spits back a string; you can thus pipe it through several iterations to add multiple colors or other attributes. Here is the code for the event log table:
#gather 20 events from the system log and pick out a few properties $events = Get-EventLog -LogName System -Newest 20 | select TimeGenerated, EventID, EntryType, UserName, Message #Create the HTML table without alternating rows, colorize Warning and Error messages, highlighting the whole row. $eventTable = $events | New-HTMLTable -setAlternating $false | Add-HTMLTableColor -Argument "Warning" -Column "EntryType" -AttrValue "background-color:#FFCC66;" -WholeRow | Add-HTMLTableColor -Argument "Error" -Column "EntryType" -AttrValue "background-color:#FFCC99;" -WholeRow #Build the HTML head, add an h3 header, add the event table, and close out the HTML $HTML = New-HTMLHead $HTML += "<h3>Last 20 System Events</h3>" $HTML += $eventTable | Close-HTML #test it out set-content C:\test.htm $HTML & 'C:\Program Files\Internet Explorer\iexplore.exe' C:\test.htmIf we want to highlight multiple levels of a value, we can use the pipeline. In this example, we add boring pastels to process handle counts above 3000, 2000 and 1000:
Tying it all together
Lets use these functions in concert – the webpage this provides is somewhat useless, but you can repeat and repurpose the steps here across various solutions.
The basic workflow is as follows:
- Start the HTML
- Create and colorize HTML tables if desired
- Add content, tables and other data to the HTML
- Close the HTML
If you are interested in using PowerShell for reports and other solutions like this, I would highly recommend checking out the following resources: