因此,我已經制作了此腳本,可以找到機器上安裝的所有軟件版本,並讓人們知道哪些軟件以及何時安裝在多個VM上。以特定的方式從Powershell序列化JSON
我想把它放在我們使用的儀表板提供程序上,但它們有一個特定的格式來使用它。
它確實會生成一個有效的JSON,但我只是發現它不符合公司希望的格式。
具體做法是:
{"table": [["header1", "header2"], ["row1column1", "row1column2"], ["row2column1", "row2column2"]]}
我首先想到的是產生一個標題行作爲開始變量,然後各個變量的每個組件,但那種感覺非常繁瑣和費力的爲每個單獨的行創建變量的數據(日期,軟件名稱等)。然後在最後它們合併爲1,並轉換爲JSON
我的腳本是這樣的:
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline = $true,
ValueFromPipelinebyPropertyName = $true)]
[Alias("Servers")]
[string[]]$Name = (Get-Content "c:\utils\servers.txt")
)
Begin {
}
Process {
$AllComputers = @()
#Gather all computer names before processing
ForEach ($Computer in $Name) {
$AllComputers += $Computer
}
}
End {
ForEach ($Computer in $AllComputers) {
write-output "Checking $computer"
if ($computer -like "*x86*") {
$data = Invoke-Command -cn $computer -ScriptBlock {Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object @{Label = "ServerName"; Expression = {$env:computername}}, DisplayName, Publisher, DisplayVersion, InstallDate | Where-object { $_.Publisher -match "Foobar" }}
$jsondata += $data
}
else {
$data = Invoke-Command -cn $computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object @{Label = "ServerName"; Expression = {$env:computername}}, DisplayName, Publisher, DisplayVersion, InstallDate | Where-object { $_.Publisher -match "foobar" } }
$jsondata += $data
}
}
$jsondata | ConvertTo-Json -depth 100 | Out-File "\\servername\C$\Utils\InstalledApps.json"
}
「{」table「:[[」header1「,」header2「]'部分需要逐字還是隻需要遵循該格式?所有參賽作品最終都會放在一張桌子上? – Matt
不幸它是必需的。 Header1 Header2可以是任何值,但必須包含表格 – Ericrs