Home > front end >  PowerShell Multiple Arrays into a Custom HTML Table
PowerShell Multiple Arrays into a Custom HTML Table

Time:10-27

Good Morning all. I’m working on a script that updates a set of applications and I’m trying to make a HTML table for verification purposes. This comment is my framework

https://www.reddit.com/r/PowerShell/comments/3zai30/html_report_from_multiple_arrays/cyl9k47/?utm_source=share&utm_medium=ios_app&utm_name=iossmf&context=3

It works great for the most part. But it’s built off integers, so if I have an app fail to install/update, it messes up the placement of items. I’m looking for some insight into making it more of a static table so if an app fails to install/update it just is a blank line instead.

Function Get-Verification{
$Header = '<style>
    body {
        background-color: Gainsboro;
        font-family:      "Calibri";
    }

    table {
        border-width:     1px;
        border-style:     solid;
        border-color:     black;
        border-collapse:  collapse;
        width:            75%;
    }

    th {
        border-width:     1px;
        padding:          5px;
        border-style:     solid;
        border-color:     black;
        background-color: #98C6F3;
    }

    td {
        border-width:     1px;
        padding:          5px;
        border-style:     solid;
        border-color:     black;
        background-color: White;
    }

    tr {
        text-align:       left;
    }
</style>'

    
    [System.Collections.ArrayList]$SoftwareNames = @(
    "Notepad  ",
    "PowerShell7",
    "RACTools",
    "Rufus",
    "RuTTY",
    "WinSCP",
    "WireShark",
    "Microsoft Edge",
    "Chrome Portable",
    "FireFox Portable",
    "NetBanner",
    "OVFTool",
    "PowerCLI",
    "VMRC",
    "Workstation",
    "Axway",
    "InstallRoot",
    "ActivClient",
    "90Meter",
    "Microsoft Office"
    )
    
    [System.Collections.ArrayList]$NewSWVersions = @(
    "$NotepadVersion",
    "$PowerShellVersion",
    "$RACToolsVersion",
    "$RufusVersion",
    "$RuTTYVersion",
    "$WinSCPVersion",
    "$WireSharkVersion",
    "$MSEdgeVersion", 
    "$ChromeVersion",
    "$FireFoxVersion",
    "$NetBannerVersion",
    "$OVFToolMsiVersion",
    "$PowerCLIVersion",
    "$VMRCVersion",
    "$WorkstationVersion",
    "$AxwayVersion",
    "$InstallRootMsi",
    "$ACVersion",
    "$90MVersion",
    "$Office" #Needs fixing
    )
    
    [System.Collections.ArrayList]$InstalledVersions = @(
    "$NotepadExe",
    "$PowerShellExe",
    "$RACToolsExe",
    "$RufusVersion",
    "$RuTTYExe",
    "$WinSCPExe",
    "$WireSharkExe",
    "$MSEdgeExe", 
    "$ChromeVersion",
    "$FireFoxVersion",
    "$NetBannerExe",
    "$OVFToolExe",
    "$PowerCLIModule",
    "$VMRCExe",
    "$WorkstationExe",
    "$AxwayExe",
    "$InstallRootExe",
    "$ACExe",
    "$90MExe",
    "$Office" #needs fixing
    )
    
    If($Class -eq "NIPR"){
        $SoftwareNames.Remove("90Meter")
        $NewSWVersions.Remove("$90MVersion")
        $InstalledVersions.Remove("$90MExe")
    }
    
    If($Class -eq "SIPR"){
        $SoftwareNames.Remove("ActivClient")
        $NewSWVersions.Remove("$ACVersion")
        $InstalledVersions.Remove("$ACExe")
    }   
    
    $i = 0
    
    $(
    While (
        @(
            $SoftwareNames[$i],
            $NewSWVersions[$i],
            $InstalledVersions[$i]
        ) -ne $null
    ) {
        $Properties = [ordered]@{
            "Software Names" = $SoftwareNames[$i]
            "Expected Version" = $NewSWVersions[$i]
            "Installed Version" = $InstalledVersions[$i]
        }
        New-Object psobject -Property $Properties
        $i  
    }
) | ConvertTo-Html -head $Header -PostContent "Report Run on $Date"| Out-File "App_Verification.htm"
invoke-item "App_Verification.htm"
    exit
    
}

The information I’m tabling is Application Name Expected Version Installed Version

They are all arrays with variables that pull the required information.

CodePudding user response:

Use an ordered dictionary to store the information instead of separate arrays:

$software = [ordered]@{
    "Notepad  " = [pscustomobject]@{
        Application = "Notepad  "
        NewVersion = "$NotepadVersion"
        InstalledVersion = "$NotepadExe"
    }
    "PowerShell7" = [pscustomobject]@{
        Application = "PowerShell7"
        NewVersion = "$PowerShellVersion"
        InstalledVersion = "$PowerShellExe"
    }
    # ... and so on
}

Removing an application from the list then becomes:

If($Class -eq "NIPR"){
    # remove the whole object from the dictionary, no need to worry about multiple collections
    $software.Remove("90Meter")
}

And modifying the details of one is just a matter of referencing it by name and modifying the correct property:

$software['PowerShell7'].NewVersion = "new version goes here"

And since they're already objects now, exporting them to HTML is as easy as:

$software.Values |ConvertTo-Html ...
  • Related