【问题标题】:Update JSON file parameters daily using Powershell script每天使用 Powershell 脚本更新 JSON 文件参数
【发布时间】:2019-09-12 02:19:16
【问题描述】:

我有一个 JSON 文件,其中包含我用来从 Oracle 提取审计历史数据的查询。我需要能够每天自动更改fromDatetoDate 参数。我正在考虑创建一个 powershell 脚本并使用:Get-Date -format "yyyy-MM-dd" 作为变量,但不确定这是最好的方法吗?我的 JSON 文件 (Oracle.json) 如下所示:

{
  "fromDate": "2019-04-18",
  "toDate": "2019-04-18",
  "product": "OPSS",
  "eventType": "RoleMembershipAdd"
}

然后我使用 curl 发出 POST 请求并将数据输出到平面文件:

curl.exe -i --user username:password -X POST -H "Content-Type: application/json" -d "@C:\Oracle.json" hxxps://someurl.com/fscmRestApi/fndAuditRESTService/audittrail/getaudithistory >> C:\Oracle.txt

让日期动态化的最佳方法是什么,这样我就可以让脚本每天运行并从当天拉取,而无需手动更改日期?

【问题讨论】:

    标签: json powershell curl


    【解决方案1】:

    我会使用这里的字符串并直接插入日期:

    $Json = @"
    {
      "fromDate": "$(get-date -format "yyyy-MM-dd")",
      "toDate": "$(get-date -format "yyyy-MM-dd")",
      "product": "OPSS",
      "eventType": "RoleMembershipAdd"
    }
    "@ | ConvertFrom-Json | ConvertTo-Json -Compress
    

    > $Json
    "fromDate":"2019-04-23","toDate":"2019-04-23","product":"OPSS","eventType":"RoleMembershipAdd"}
    

    【讨论】:

    • 非常感谢!!!我花了一点时间弄清楚如何通过 curl 传递它。我最终使用了:-d“@-”。所以它看起来像下面这样: curl.exe -i --user username:password -X POST -H "Content-Type: application/json" -d "@-" hxxps://someurl.com/fscmRestApi/fndAuditRESTService/ audittrail/getaudihistory >> C:\Oracle.txt。出于某种原因,它不会使用:-d $Json。再次感谢!
    • 感谢您的反馈,对新用户的提示:如果答案解决了您的问题或您觉得它有帮助,您应该考虑accept the answer 和/或vote up(一旦你有一个@ 987654323@ of 15 将会看到)
    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2017-11-17
    • 2019-04-17
    • 2016-06-22
    • 2018-11-11
    • 1970-01-01
    相关资源
    最近更新 更多