powershell - 使用PowerShell替换JSON中的值

标签 powershell replace

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

{
"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中提到)。

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

最佳答案

该脚本将帮助您更新日志目录。

脚步:

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

  • 脚本:
    $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 - 使用PowerShell替换JSON中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50521035/

    相关文章:

    powershell - 从TFSBuild.Proj文件运行Powershell脚本

    powershell - 如何访问 IIS : drive on remote machine via Powershell?

    git - git diff发布标记和commitID

    javascript - AngularJS - 如果包含字符串,则删除 ng-repeat 项

    python - 替换txt文件中的整行

    javascript - 为bracket.js制作一个插入函数

    python - 有没有办法以编程方式删除 Azure 中的 SQL 数据库?

    PowerShell 远程处理工作组计算机

    javascript - 将变量从脚本替换为同一页面上的另一个变量(JS/PHP)

    基于另一个向量替换向量中的值