Function to convert PowerShell objects into an HTML table with the option to format individual table cells based on property values using CSS selectors.
format output\ConvertTo-HtmlConditionalFormat.ps1
ConvertTo-HtmlConditionalFormat [[-InputObject] <Object>] [[-ConditionalFormat] <Hashtable>] [[-Path] <Object>]
[-Open]
Individual table cells can be formatted using a hashtable with one or multiple condition (of property to be met)/Property/css style to apply. (see example)
$Path="$env:TEMP\test.html"
#build the hashtable with Condition (of property to be met)/Property/css style to apply
$ht=@{}
$upperLimit=1000
$ht.Add("\[int\]\`$_.Value -gt $upperLimit",("Handles","color:green;font-weight: bold"))
$ht.Add('\[int\]$_.Value -lt 50',("Handles","background-color:red"))
$ht.Add('$_.Value -eq "rundll32"',("Name","background-color:blue"))
ConvertTo-HtmlConditionalFormat (Get-Process | select Name, Handles) $ht $Path -open
$Path="$env:TEMP\test.html"
#build the hashtable with Condition (of property to be met)/Property/css style to apply
$ht=@{}
$ht.Add('$_.Value -eq ".txt"',("Extension","background-color:blue"))
$ht.Add('$_.Value -eq ".bmp"',("Extension","background-color:red"))
ConvertTo-HtmlConditionalFormat (dir | select FullName,Extension,Length,LastWriteTime) $ht $Path -open
$Path="$env:TEMP\test.html"
#create some test object with a 'Compliant' property
$WindowsFeaturesCompliance =
foreach ($i in 1..10){
$compliance = "***NON COMPLIANT***"
if ($i % 2){
$compliance = "Compliant"
}
New-Object PSObject -Property @{'ItemNumber'=$i;'Compliant'=$compliance}
}
$ht=@{}
$NonCompliant = "***NON COMPLIANT***"
$ht.Add("\`$_.Value -like '$NonCompliant'",("Compliant","color:Red;font-weight: bold"))
ConvertTo-HtmlConditionalFormat ($WindowsFeaturesCompliance) $ht $Path -open
The object(s) to convert into a HTML table (no pipeline input).
Type: Object
Parameter Sets: (All)
Aliases:
Required: False
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseA hashtable with entries following the following format (see example): - Key = Predicate representing the condition as string - Value = String array with two entries - Property to be formatted - Format as CSS selector
Type: Hashtable
Parameter Sets: (All)
Aliases:
Required: False
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False{{Fill Path Description}}
Type: Object
Parameter Sets: (All)
Aliases:
Required: False
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False{{Fill Open Description}}
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False