PowerShell module to spice up ad hoc notifications and reports
PowerShell
Permalink
Failed to load latest commit information.
Media
PSHTMLTable
Tests
LICENSE
README.md
appveyor.yml
build.ps1
deploy.psdeploy.ps1
psake.ps1

README.md

PSHTMLTable

This is an old set of functions used to generate HTML tables, and highlight cells within them on demand.

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. The attached functions can be used to spice up various notifications, reports, or other HTML generated by PowerShell.

A quick example showing a standard SCOM alert, and a SCOM alert generated using PSHTMLTable functions. The latter is a bit more helpful!

Plain SCOM Alert:

plain alert

PSHTMLTable SCOM Alert:

scom spicy

Instructions

# One time setup
    # Download the repository
    # Unblock the zip
    # Extract the PSHTMLTable folder to a module path (e.g. $env:USERPROFILE\Documents\WindowsPowerShell\Modules\)

    #Simple alternative, if you have PowerShell 5, or the PowerShellGet module:
        Install-Module PSHTMLTable

# Import the module.
    Import-Module PSHTMLTable    #Alternatively, Import-Module \\Path\To\PSHTMLTable

# Get commands in the module
    Get-Command -Module PSHTMLTable

Examples

Simple Example

This illustrates creating a webpage with several Process related stats, using all of the functions in PSHTMLTable

#gather 20 events from the system log and pick out a few properties 
    $events = Get-EventLog -LogName System -Newest 20 | select TimeGenerated, Index, 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.htme2

example output

Complex Example

This illustrates creating a webpage with several Process related stats, using all of the functions in PSHTMLTable

#This example demonstrates using the New-HTMLHead, New-HTMLTable, Add-HTMLTableColor, ConvertTo-PropertyValue and Close-HTML functions 

#get processes to work with 
    $processes = Get-Process 

#Build HTML header 
    $HTML = New-HTMLHead -title "Process details" 

#Add CPU time section with top 10 PrivateMemorySize processes.  This example does not highlight any particular cells 
    $HTML += "<h3>Process Private Memory Size</h3>" 
    $HTML += New-HTMLTable -inputObject $($processes | sort PrivateMemorySize -Descending | select name, PrivateMemorySize -first 10) 

#Add Handles section with top 10 Handle usage. 
$handleHTML = New-HTMLTable -inputObject $($processes | sort handles -descending | select Name, Handles -first 10) 

    #Add highlighted colors for Handle count 

        #build hash table with parameters for Add-HTMLTableColor.  Argument and AttrValue will be modified each time we run this. 
        $params = @{ 
            Column = "Handles" #I'm looking for cells in the Handles column 
            ScriptBlock = {[double]$args[0] -gt [double]$args[1]} #I want to highlight if the cell (args 0) is greater than the argument parameter (arg 1) 
            Attr = "Style" #This is the default, don't need to actually specify it here 
        } 

        #Add yellow, orange and red shading 
        $handleHTML = Add-HTMLTableColor -HTML $handleHTML -Argument 1500 -attrValue "background-color:#FFFF99;" @params 
        $handleHTML = Add-HTMLTableColor -HTML $handleHTML -Argument 2000 -attrValue "background-color:#FFCC66;" @params 
        $handleHTML = Add-HTMLTableColor -HTML $handleHTML -Argument 3000 -attrValue "background-color:#FFCC99;" @params 

    #Add title and table 
    $HTML += "<h3>Process Handles</h3>" 
    $HTML += $handleHTML 

#Add process list containing first 10 processes listed by get-process.  This example does not highlight any particular cells 
    $HTML += New-HTMLTable -inputObject $($processes | select name -first 10 ) -listTableHead "Random Process Names" 

#Add property value table showing details for PowerShell ISE 
    $HTML += "<h3>PowerShell Process Details PropertyValue table</h3>" 
    $processDetails = Get-process powershell_ise | select name, id, cpu, handles, workingset, PrivateMemorySize, Path -first 1 
    $HTML += New-HTMLTable -inputObject $(ConvertTo-PropertyValue -inputObject $processDetails) 

#Add same PowerShell ISE details but not in property value form.  Close the HTML 
    $HTML += "<h3>PowerShell Process Details object</h3>" 
    $HTML += New-HTMLTable -inputObject $processDetails | Close-HTML 

#write the HTML to a file and open it up for viewing 
    set-content C:\test.htm $HTML 
    & 'C:\Program Files\Internet Explorer\iexplore.exe' C:\test.htm 

example output

Functions

PSHTMLTable includes several functions:

  • ConvertTo-PropertyValue - Convert an object with various properties into an array of property,value pairs
  • New-HTMLHead - Starts building HTML including internal style sheet (leaves body, html tags open)
  • New-HTMLTable - Create an HTML table from one or more objects
  • Add-HTMLTableColor - Colorize cells or rows in an HTML table, or add other inline CSS
  • CloseHTML - Close out the body and html tags

Notes

This was written during my early days with PowerShell - pull requests would be welcome!

For more details, stop by the old blog post on this!