3

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.

How can this be done using powershell?

2
  • 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. Commented May 25, 2018 at 6:11
  • There are two asks here, one is the json replace and the other is the daily task. Which is it? It is best to keep questions to one specific topic. Commented Dec 20, 2022 at 17:52

3 Answers 3

9

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 
Sign up to request clarification or add additional context in comments.

4 Comments

What does .update refer to and why do you need ForEach-Object (%), given that the input JSON represents a single object?
What is the -Depth 4 do?
Default value of Depth is 2 and in above case attribute was at 4th depth, so we set proper depth.
5

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

Comments

3

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.