2017-04-26 7 views
0

私はユーザーデータで編集する必要のある.jsonファイルを持っていますので、これを実行するにはpowershellを使用する必要があります。 JSONはこのようなものになります。powershellを使用して.jsonファイルを編集する

{ 
"EngineConfiguration": { 
    "PollInterval": "00:00:15", 
    "Components": [ 
     { 
      "Id": "CustomLogs", 
      "FullName": "AWS.EC2.Windows.CloudWatch.CustomLog.CustomLogInputComponent,AWS.EC2.Windows.CloudWatch", 
      "Parameters": { 
       "LogDirectoryPath": "C:\\CustomLogs\\", 
       "TimestampFormat": "MM/dd/yyyy HH:mm:ss", 
       "Encoding": "UTF-8", 
       "Filter": "", 
       "CultureName": "en-US", 
       "TimeZoneKind": "Local" 
      } 
     } 
    ], 
    "Flows": { 
     "Flows": 
     [ 
      "(ApplicationEventLog,SystemEventLog),CloudWatchLogs" 
      ] 
     } 
    } 
} 

私はそれは次のようになりたい - カスタムログパラメータの

{ 
"EngineConfiguration": { 
    "PollInterval": "00:00:15", 
    "Components": [ 
     { 
      "Id": "CustomLogs", 
      "FullName": "AWS.EC2.Windows.CloudWatch.CustomLog.CustomLogInputComponent,AWS.EC2.Windows.CloudWatch", 
      "Parameters": { 
       "LogDirectoryPath": "C:\\ProgramData\\Amazon\\CodeDeploy\\deployment-logs", 
       "TimestampFormat": "[yyyy-MM-dd HH:mm:ss.fff]", 
       "Encoding": "UTF-8", 
       "Filter": "", 
       "CultureName": "en-US", 
       "TimeZoneKind": "Local" 
      } 
     } 
    ], 
    "Flows": { 
     "Flows": 
     [ 
      "(ApplicationEventLog,SystemEventLog, CustomLogs),CloudWatchLogs" 
      ] 
     } 
    } 
} 

を、LogDirectoryPathとTIMESTAMPFORMATは両方変更されました。また、Flowsセクションでは、「CustomLogs」をCloudWatch Groupに追加しました。

私はそれがこのようなコードで作業作ってみました:

$a = Get-Content 'C:\PATH\TO\file.json' -raw | ConvertFrom-Json 
$a.EngineConfiguration.Components[0].Parameters = '{"LogDirectoryPath": "","TimestampFormat": "[yyyy-MM-dd HH:mm:ss.fff]","Encoding": "UTF-8","Filter": "","CultureName": "en-US","TimeZoneKind": "Local"}' 
$a | ConvertTo-Json | set-content 'C:\PATH\TO\output.json' 

しかし、それは非常に醜い出力

{ 
"EngineConfiguration": { 
          "PollInterval": "00:00:15", 
          "Components": [ 
               "@{Id=CustomLogs; FullName=AWS.EC2.Windows.CloudWatch.CustomLog.CustomLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters={\"LogDirectoryPath\": \"\",\"TimestampFormat\": \"[yyyy-MM-dd HH:mm:ss.fff]\",\"Encoding\": \"UTF-8\",\"Filter\": \"\",\"CultureName\": \"en-US\",\"TimeZoneKind\": \"Local\"}}", 
               "@{Id=CloudWatchLogs; FullName=AWS.EC2.Windows.CloudWatch.CloudWatchLogsOutput,AWS.EC2.Windows.CloudWatch; Parameters=}" 
              ], 
          "Flows": { 
              "Flows": "(ApplicationEventLog,SystemEventLog),CloudWatchLogs" 
             } 
         } 
} 

はこれを行うにはよりエレガントな方法はあります生成?アドバイスをいただければ幸いです。ありがとう!

答えて

2

ConvertTo-Jsonため-Depthスイッチを使用してみてください。デフォルトでは、これはあなたが見ているオブジェクトの文字列表現に2の深さを越えた子要素を圧縮:

"@{Id=CustomLogs; etc." 

深い深さを指定することで、あなたがより多くのあなたが望むような形式を取得します。余分な空白を以下のように圧縮するものと組み合わせてください:

((ConvertFrom-Json $a) | ConvertTo-Json -Depth 4) -replace ((" "*4)," ") 
0

正規表現で先行する空白を減らすことができます。しかし、それは実際にあなたが望むと言っている再フォーマット、かわいいプリントを生み出すものではありません。

$a | ConvertTo-Json | % {$_ -replace "  "," "} | set-content 'output.json' 
関連する問題