このスクリプトを実行すると、マシンにインストールされているすべてのソフトウェアバージョンが検出され、複数のVMにインストールされたソフトウェアとソフトウェアを知ることができます。PowershellのJSONを特定の方法でシリアライズ
私たちが使用するダッシュボードプロバイダに入れたいのですが、使用する特定の形式があります。
有効なJSONを生成しますが、会社が望む形式ではないことがわかりました。あろう
:
{"table": [["header1", "header2"], ["row1column1", "row1column2"], ["row2column1", "row2column2"]]}
私の最初の考えは、開始変数と各成分の個々の変数としてヘッダー行を生成することであろうが、それは、個々の行のための変数を作成することは非常に退屈で面倒に感じデータ(日付、ソフトウェア名など)。私はあなたが配列の配列を探していると結論付けてしまう提供されるサンプル出力フォーマットから
[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 "]'は部分的に必須か、その形式に従うだけですか?すべてのエントリは1つのテーブルに終わるでしょうか? – Matt
不運にも必要です。 Header1 Header2は任意の値にすることができますが、テーブルを含める必要があります – Ericrs