2017-09-19 8 views
0

jsonデータの書式設定に関する助けが必要です。私はテーブルをjsonに変換してログコレクタに取り込む。コレクターはjsonだけをシングルライン形式で好きです。どのように私はこのJSONを変換することができます:Powershell Json書式設定

[ 
    { "Severity": "Informational", 
     "Type": "Milestone", 
     "SiteCode": "ABC", 
     "DateTime": 1505840220813, 
     "System": "Server.domain.local", 
     "Component": "SMS_Distribution_Point_Monitoring", 
     "Module": "SMS Server", 
     "MessageID": 2380, 
     "Description": "Start to evaluate package ABC001F5 on distribution point Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\." 
    }, 
    { "Severity": "Informational", 
     "Type": "Milestone", 
     "SiteCode": "ABC", 
     "DateTime": 1505840220787, 
     "System": "Server.domain.local", 
     "Component": "SMS_Distribution_Point_Monitoring", 
     "Module": "SMS Server", 
     "MessageID": 2384, 
     "Description": "Package ABC0019F on distribution point Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\ has been verified successfully." 
    } 
] 

これにPowerShellでファイルに出力するとき:あなたは、文字列を作業している場合

[{"Severity":"Informational","Type":"Milestone","SiteCode":"ABC","DateTime":1505840220813,"System":"Server.thecarlylegroup.local","Component":"SMS_Distribution_Point_Monitoring","Module":"SMSServer","MessageID":2380,"Description":"StarttoevaluatepackageABC001F5ondistributionpointDisplay=\\\\Server.thecarlylegroup.local\\MSWNET:SMS_SITE=ABC\\\\Server.thecarlylegroup.local\\."}, 
{"Severity":"Informational","Type":"Milestone","SiteCode":"ABC","DateTime":1505840220787,"System":"Server.thecarlylegroup.local","Component":"SMS_Distribution_Point_Monitoring","Module":"SMSServer","MessageID":2384,"Description":"PackageABC0019FondistributionpointDisplay=\\\\Server.thecarlylegroup.local\\MSWNET:SMS_SITE=ABC\\\\Server.thecarlylegroup.local\\hasbeenverifiedsuccessfully."}] 

答えて

0

$var = (@" 
[ 
    { "Severity": "Informational", 
     "Type": "Milestone", 
     "SiteCode": "ABC", 
     "DateTime": 1505840220813, 
     "System": "Server.domain.local", 
     "Component": "SMS_Distribution_Point_Monitoring", 
     "Module": "SMS Server", 
     "MessageID": 2380, 
     "Description": "Start to evaluate package ABC001F5 on distribution point Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\." 
    }, 
    { "Severity": "Informational", 
     "Type": "Milestone", 
     "SiteCode": "ABC", 
     "DateTime": 1505840220787, 
     "System": "Server.domain.local", 
     "Component": "SMS_Distribution_Point_Monitoring", 
     "Module": "SMS Server", 
     "MessageID": 2384, 
     "Description": "Package ABC0019F on distribution point Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\ has been verified successfully." 
    } 
] 
"@ -split "`n" | % { $_.Trim() }) -join '' 
+0

それは私が実際に出力してい – Patrick

0

あなたを取得すると仮定すると、テーブルのデータを次のような文字列に変換します。

$json = @" 
[ 
    { "Severity": "Informational", 
     "Type": "Milestone", 
     "SiteCode": "ABC", 
     "DateTime": 1505840220813, 
     "System": "Server.domain.local", 
     "Component": "SMS_Distribution_Point_Monitoring", 
     "Module": "SMS Server", 
     "MessageID": 2380, 
     "Description": "Start to evaluate package ABC001F5 on distribution point Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\." 
    }, 
    { "Severity": "Informational", 
     "Type": "Milestone", 
     "SiteCode": "ABC", 
     "DateTime": 1505840220787, 
     "System": "Server.domain.local", 
     "Component": "SMS_Distribution_Point_Monitoring", 
     "Module": "SMS Server", 
     "MessageID": 2384, 
     "Description": "Package ABC0019F on distribution point Display=\\\\Server.domain.local\\ MSWNET: SMS_SITE=ABC \\\\Server.domain.local\\ has been verified successfully." 
    } 
] 
"@ 

そして、あなたは(深さはオブジェクト全体が変換されたことを確認するために、任意に選択された)これを行うことができます。

$compressedJson = $json | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 100 
+0

を述べているように、その文字列ではなく、そのテーブルをJSONに変換されますJsonに送信し、無効なデータも削除します。 $ StatusMessagesJson = $ StatusMessages | (Regex)::エスケープ( ')\/"')、" "| | | | | | | | | | | | | | | | | | | | | | | | |セットコンテンツ$パス – Patrick