Effortlessly Convert PowerShell Objects to Stylish HTML Tables with NinjaOne

Effortlessly Convert PowerShell Objects to Stylish HTML Tables with NinjaOne

PowerShell is an incredibly powerful tool for IT professionals, enabling the automation of tasks and the handling of data in complex ways. However, presenting this data in a user-friendly manner can often be a challenge. Enter the ConvertTo-HtmlTable function – a lifesaver for those who frequently need to display PowerShell data in HTML format, especially when using NinjaOne's custom fields with WYSIWYG styling.

What Does ConvertTo-HtmlTable Do?

The ConvertTo-HtmlTable function takes an array of objects and elegantly converts it into an HTML table. This functionality is perfect for IT admins and professionals who need to present data in a more readable and presentable format. Whether you're reporting system statistics, user information, or security vulnerabilities, this function makes it simple.

Key Features

  • Ease of Use: With a straightforward syntax, the function can be easily integrated into your existing PowerShell scripts.
  • Customizable Row Coloring: Set the RowColour property to control the styling of each row in your table, allowing for a clear visual hierarchy.
  • Dynamic Table Creation: Automatically generates table headers and rows based on the object's properties.

Code of the Function

You can always find the latest version here: https://github.com/freezscholte/Public-Ninja-Scripts/blob/main/WYSIWYG-Styling/Function-CreateTable.ps1

function ConvertTo-HtmlTable {
    param (
        [Parameter(Mandatory=$true)]
        [System.Collections.ArrayList]$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()
}

How to Use the Function

Using the ConvertTo-HtmlTable function is as simple as passing your array of objects to it. Here’s a quick example where $ArrayObject is a collection of Powershell objects.

$htmlTable = ConvertTo-HtmlTable -Objects $ArrayObject
$output = "@'
$($htmlTable)
'@"

Ninja-Property-Set cveTable $output

Real-World Application: Tracking Security Vulnerabilities

Imagine you have a list of security vulnerabilities from a scan, and you want to classify them by severity. With ConvertTo-HtmlTable, you can easily create an HTML table that highlights each vulnerability in colors corresponding to their severity level – 'HIGH' as red, 'MEDIUM' as yellow, and so on, see code snippet below for an example. This visual aid can be instrumental in prioritizing issues for your IT security team.

#Within the script logic you can use an switch statement for setting the color of he RowColour in your PSCustomObject

$RowColour = switch ($Object.vulnerabilities.cve.metrics.cvssMetricV31.cvssdata.baseSeverity) {
    "HIGH" { "danger" }
    "MEDIUM" { "warning" }
    "LOW" { "other" }
    default { $null } # Fallback to $Null in case it doesn't match any case or colour
}


$CVEResult = [PSCustomObject]@{
    'CVE'                 = $cveId
'ExploitabilityScore' = $Object.vulnerabilities.cve.metrics.cvssMetricV31.exploitabilityScore
    'ImpactScore'         = $Object.vulnerabilities.cve.metrics.cvssMetricV31.impactScore
    'AttackVector'        = $Object.vulnerabilities.cve.metrics.cvssMetricV31.cvssdata.attackVector
    'AttackComplexity'    = $Object.vulnerabilities.cve.metrics.cvssMetricV31.cvssdata.attackComplexity
    'PrivilegesRequired'  = $Object.vulnerabilities.cve.metrics.cvssMetricV31.cvssdata.privilegesRequired
    'IntegrityImpact'     = $Object.vulnerabilities.cve.metrics.cvssMetricV31.cvssdata.integrityImpact
    'UserInteraction'     = $Object.vulnerabilities.cve.metrics.cvssMetricV31.cvssdata.userInteraction
    'BaseScore'           = $Object.vulnerabilities.cve.metrics.cvssMetricV31.cvssdata.baseScore
    'BaseSeverity'        = $Object.vulnerabilities.cve.metrics.cvssMetricV31.cvssdata.baseSeverity
    'RowColour'           = $RowColour
}

Modifying the Function

The provided script is open for modifications. If you have additional requirements or need to include more data points, feel free to adjust the code. And if you encounter any roadblocks or have suggestions, I’m all ears – reach out anytime!

PowerShell Builtin option

Another option is to use the 'ConvertTo-Html -Fragment' builtin cmdlet. This will just output a raw HTML table that will work with the WYSIWYG fields in NinjaOne but you don't have any control in setting row colors.

Link to cmdlet documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-html?view=powershell-5.1

Conclusion

With ConvertTo-HtmlTable, converting PowerShell objects to an HTML table is no longer a daunting task. This function streamlines the process, ensuring your data is presented in a clear, concise, and visually appealing manner. It’s another step towards efficient IT management, courtesy of the powerful combination of PowerShell and NinjaOne.