Public/New-HTMLHead.ps1
function New-HTMLHead { <# .SYNOPSIS Returns HTML including internal style sheet .DESCRIPTION Returns HTML including internal style sheet .PARAMETER cssPath If specified, contents of this file are embedded in an internal style sheet via <style> tags Note: If you include your own CSS, please note that the New-HTMLTable function looks for 'odd' and 'even' class names. Note .odd and .even defitions in $HTMLStyle. .PARAMETER title If specified, title to add in the head section .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 .FUNCTIONALITY General Command #> [cmdletbinding(DefaultParameterSetName="String")] param( [Parameter(ParameterSetName='File')] [validatescript({test-path $_ -pathtype leaf})]$cssPath = $null, [Parameter(ParameterSetName='String')] [string]$style = "<style> body { color:#333333; font-family:Calibri,Tahoma,arial,verdana; font-size: 11pt; } h1 { text-align:center; } h2 { border-top:1px solid #666666; } table { border-collapse:collapse; } th { text-align:left; font-weight:bold; color:#eeeeee; background-color:#333333; border:1px solid black; padding:5px; } td { padding:5px; border:1px solid black; } .odd { background-color:#ffffff; } .even { background-color:#dddddd; } </style>", [string]$title = $null ) #add css from file if specified if($cssPath){$style = "<style>$(get-content $cssPath | out-string)</style>"} #Return HTML @" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> $(if($title){"<title>$title</title>"}) $style </head> <body> "@ } |