PowerShell 将多个数组转换为自定义 HTML Table

PowerShell Multiple Arrays into a Custom HTML Table

大家早上好。我正在编写一个更新一组应用程序的脚本,并且我正在尝试制作一个 HTML table 用于验证目的。这个评论是我的框架

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

大部分情况下效果很好。但它是基于整数构建的,所以如果我有一个应用程序无法 install/update,它会弄乱项目的放置。我正在寻找使它更像静态 table 的一些见解,因此如果应用程序无法 install/update 它只是一个空行。

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",
    "MVersion",
    "$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",
    "MExe",
    "$Office" #needs fixing
    )
    
    If($Class -eq "NIPR"){
        $SoftwareNames.Remove("90Meter")
        $NewSWVersions.Remove("MVersion")
        $InstalledVersions.Remove("MExe")
    }
    
    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
    
}

我提供的信息是应用程序名称预期版本安装版本

它们都是带有变量的数组,可以提取所需的信息。

使用有序字典而不是单独的数组来存储信息:

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

从列表中删除一个应用程序会变成:

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

而修改其中一个的详细信息只是通过名称引用它并修改正确的问题 属性:

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

并且由于它们现在已经是对象,将它们导出到 HTML 非常简单:

$software.Values |ConvertTo-Html ...