html - PowerShell 多个数组到自定义 HTML 表中
问题描述
大家早上好。我正在编写一个更新一组应用程序的脚本,并且我正在尝试制作一个 HTML 表格以进行验证。这条评论是我的框架
它在大多数情况下都很好用。但它是基于整数构建的,所以如果我有一个应用程序无法安装/更新,它会打乱项目的位置。我正在寻找一些使其更像静态表的洞察力,因此如果应用程序无法安装/更新它只是一个空白行。
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 ...
推荐阅读
- image - 如何使用 JuicyPixels (Haskell) 编辑 DynamicImage 的像素?
- sql-server - 在新页面顶部重复行
- r - 为什么“FALSE”在分配为向量的一部分时变为零
- javascript - chrome 扩展中的 webkitSpeechRecognition“不允许”错误
- r - SEM 在 Lavaan 的模型拟合
- javascript - 为什么每次组件重新渲染时都会运行useEffect?
- javascript - 使用 reduce() 与 forEach() 的非原始数组元素的总和
- ubuntu - 如何在 Ubuntu 上以八度音阶创建旋转矩阵?
- r - 模拟结果的争论
- java - 在特定条件下使用输入文件通过命令行运行已编译的 java 文件