使用powershell替换json中的值 [英] Replace a value in a json using powershell

查看:94
本文介绍了使用powershell替换json中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的 json 文件 -

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"
        }
    }]
  }
}

我想每天使用使用 powershell 的托管任务替换此日期(在 LogDirectoryPath 中提到).

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

有人可以提供最简单的方法来每天使用 powershell 替换它吗?

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

推荐答案

此脚本将帮助您更新日志目录.

This script will help you to update log directory.

步骤:

  1. 获取json文件的内容.
  2. 更新属性值(如果存在).$JsonData.update |% {if(...)
  3. 将内容保存在同一文件中.

脚本:

$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 

这篇关于使用powershell替换json中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆