PowerShell Multiple Arrays into a Custom HTML Table



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

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

它在大多数情况下工作得很好。但它是基于整数构建的,所以如果我的应用安装/更新失败,它就会打乱项目的位置。我正在寻找一些洞察,使其更像一个静态表,所以如果一个应用程序未能安装/更新它只是一个空白行,而不是。

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

}

我列出的信息是应用程序名称期望版本安装版本

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

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

$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 ...

最新更新