Public/New-HTMLTable.ps1
function New-HTMLTable { <# .SYNOPSIS Create an HTML table from an input object .DESCRIPTION Create an HTML table from an input object .PARAMETER InputObject One or more objects (ie. (Get-process | select Name,Company) .PARAMETER Properties If specified, limit table to these specific properties in the order specified. .PARAMETER setAlternating Add CSS class = odd or even to each row. True by default. Be sure your CSS includes odd and even definitions .PARAMETER listTableHead If a list is provided, use this parameter to specify the list header (PowerShell uses * by default) .EXAMPLE #This example requires and 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 .NOTES Props to Zachary Loeber for the idea: http://gallery.technet.microsoft.com/scriptcenter/Colorize-HTML-Table-Cells-2ea63acd I believe that .Net 3.5 is a requirement for using the Linq libraries .FUNCTIONALITY General Command #> [CmdletBinding()] param ( [Parameter( Position=0, Mandatory=$true, ValueFromPipeline=$true)] [PSObject[]]$InputObject, [Parameter( Mandatory=$false, ValueFromPipeline=$false)] [string[]]$Properties, [Parameter( Mandatory=$false, ValueFromPipeline=$false)] [bool]$setAlternating = $true, [Parameter( Mandatory=$false, ValueFromPipeline=$false)] [string]$listTableHead = $null ) BEGIN { #requires -version 2.0 add-type -AssemblyName System.xml.linq | out-null $Objects = New-Object System.Collections.ArrayList } PROCESS { #Loop through inputObject, add to collection. Filter properties if specified. foreach($object in $inputObject){ if($Properties){ [void]$Objects.add(($object | Select $Properties)) } else{ [void]$Objects.add( $object )} } } END { # Convert our data to x(ht)ml $xml = [System.Xml.Linq.XDocument]::Parse("$($Objects | ConvertTo-Html -Fragment)") #replace * as table head if specified. Note, this should only be done for a list... if($listTableHead){ $xml = [System.Xml.Linq.XDocument]::parse( $xml.Document.ToString().replace("<th>*</th>","<th>$listTableHead</th>") ) } if($setAlternating){ #loop through descendents. If their index is even mark with class even, odd with class odd. foreach($descendent in $($xml.Descendants("tr"))){ if(($descendent.NodesBeforeSelf() | Measure-Object).count % 2 -eq 0){ $descendent.SetAttributeValue("class", "even") } else{ $descendent.SetAttributeValue("class", "odd") } } } #Provide full HTML or just the table depending on param $xml.Document.ToString() } } |