Public/ConvertTo-ObjectToHtmlTable.ps1
function ConvertTo-ObjectToHtmlTable { param ( [Parameter(Mandatory=$true)] [System.Collections.Generic.List[Object]]$Objects ) $sb = New-Object System.Text.StringBuilder # Start the HTML table [void]$sb.Append('<table><thead><tr>') # Add column headers based on the properties of the first object, excluding "RowColour" $Objects[0].PSObject.Properties.Name | Where-Object { $_ -ne 'RowColour' } | ForEach-Object { [void]$sb.Append("<th>$_</th>") } [void]$sb.Append('</tr></thead><tbody>') foreach ($obj in $Objects) { # Use the RowColour property from the object to set the class for the row $rowClass = if ($obj.RowColour) { $obj.RowColour } else { "" } [void]$sb.Append("<tr class=`"$rowClass`">") # Generate table cells, excluding "RowColour" foreach ($propName in $obj.PSObject.Properties.Name | Where-Object { $_ -ne 'RowColour' }) { [void]$sb.Append("<td>$($obj.$propName)</td>") } [void]$sb.Append('</tr>') } [void]$sb.Append('</tbody></table>') return $sb.ToString() } |