1
votes

I have a json file with the following content -

{
"IsEnabled": true,
"EngineConfiguration": {
    "PollInterval": "00:00:15",
    "Components": [{
        "Id": "Logs",
        "FullName": "AWS.EC2.Windows.CloudWatch.CustomLog.CustomLogInputComponent,AWS.EC2.Windows.CloudWatch",
        "Parameters": {
            "LogDirectoryPath": "C:\\log\\2018-05-25",
            "TimestampFormat": "yyyy-MM-dd HH:mm:ss",
            "Encoding": "UTF-8",
            "Filter": "",
            "CultureName": "en-US",
            "TimeZoneKind": "UTC",
            "LineCount": "1"
        }
    }]
  }
}

I want to replace this date(mentioned in LogDirectoryPath) everyday using a managed task using powershell.

Can someone please give the easiest way to replace this everyday using powershell?

3
You could create a scheduled task with (Get-Content yourfile) - replace '(?<="LogDirectoryPath": "C:\\\\log\\\\)(?<date>[\d-]+)(?=",)', (Get-Date -Format "yyyy-MM-dd") | Set-Content yourfile but you should consider using a format like TimestampFormat. Changing the JSON file daily is a code smell.Lieven Keersmaekers

3 Answers

7
votes

This script will help you to update log directory.

Steps:

  1. Get content of json file.
  2. Update attribute value if exists. $JsonData.update | % {if(...)
  3. Save content in same file.

Script:

$JsonData = Get-Content $JsonFilePath -raw | ConvertFrom-Json

$JsonData.update | % { if($JsonData.engineconfiguration.Components.Parameters.LogDirectoryPath)
                            {
                                $JsonData.engineconfiguration.Components.Parameters.LogDirectoryPath = "C:\log\$(Get-Date -Format 'yyyy-MM-dd')"
                            }
                        }

$JsonData | ConvertTo-Json -Depth 4  | set-content $JsonFilePath 
4
votes

The first step is converting the content to json:

$json = Convertfrom-json (get-content "\\path\To\Json\File.json")

Then editing the value as desired:

$json.engineconfiguration.Components.Parameters.LogDirectoryPath = "C:\log\$(Get-Date -Format 'yyyy-MM-dd')"

Then writing it back to file:

ConvertTo-Json $json -Depth 4 | Out-File C:\ProgramData\Temp\test.txt -Force
2
votes

I have also faced the same kind of issue. I was looking to change the records of the below JSON file

{
"SQS_QUEUE_URL":  "https://que-url.com/server1",
"SQS_EVENTS_QUEUE_URL":  "https://events-server.com/server1/development_events",
"REGION":  "region1",
"BUCKET":  "test-bucket",
"AE_WORK_PATH":  "C:\\workpath\\path1",
"ENV":  "env"

}

Finally, I managed to find the easiest way to generate a JSON file from Powershell.

$json = Get-Content "c:\users\bharat.gadade\desktop\test.json" | ConvertFrom-Json 
$json.SQS_QUEUE_URL = "https://que-url.com/server2"
$json.SQS_EVENTS_QUEUE_URL = "https://events-server.com/Server2/development_events"
$json.REGION = "region1 "
$json.BUCKET = "test-bucket"
$json.AE_WORK_PATH = "C:\workpath\path1"
$json.ENV = "env"
$json | ConvertTo-Json | Out-File "c:\users\bharat.gadade\desktop\test.json"