Public/New-HTMLTable.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
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() } } |