2017-10-04 27 views
1

このスクリプトを実行すると、マシンにインストールされているすべてのソフトウェアバージョンが検出され、複数の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" 
} 
+0

'{" table ":[[" header1 "、" header2 "]'は部分的に必須か、その形式に従うだけですか?すべてのエントリは1つのテーブルに終わるでしょうか? – Matt

+0

不運にも必要です。 Header1 Header2は任意の値にすることができますが、テーブルを含める必要があります – Ericrs

答えて

0

:そして最後に1にそれらを組み合わせて、

私のスクリプトをJSONに変換することはこれです。とにかくそれはテーブルオブジェクト内に必要なので、"bug" using ConvertTo-Json when trying to do thisがあります。あなたのコードを使用しているが、私のローカルコンピュータ上の例を示します。これをコードに統合することは問題ではありません。

# gather the results 
$results = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-object { $_.Publisher -match "The" } | Select-Object @{Label = "ServerName"; Expression = {$env:computername}}, DisplayName, Publisher, DisplayVersion, InstallDate 

# Prepare an array of arrays for the output. 
$outputToBeConverted = @() 

# build the header 
$header = ($results | Get-Member -MemberType NoteProperty).Name 
$outputToBeConverted += ,$header 

# Add the rows 
Foreach($item in $results){ 
    # Create a string array by calling each property individually 
    $outputToBeConverted += ,[string[]]($header | ForEach-Object{$item."$_"}) 
} 

[pscustomobject]@{table=$outputToBeConverted} | ConvertTo-Json -Depth 5 

基本的には第一部材はあなたの「ヘッダ」であり、各列は、$resultsコレクション内のアイテムから手動で構築されている配列のギザギザの配列を作っています。

上記の単項演算子,が表示されます。これは、PowerShellが配列をアンロールするのを防ぐために行われます。それがなければ、出力に1つの長い配列ができます。

+0

ありがとう私はこれを試してみましょう! – Ericrs

関連する問題