简体   繁体   English

每天使用Powershell脚本更新JSON文件参数

[英]Update JSON file parameters daily using Powershell script

I have a JSON file that contains a query I am using to pull audit history data from Oracle. 我有一个包含查询的JSON文件,我正在使用该文件从Oracle中提取审计历史记录数据。 I need to be able to automate changing the fromDate and toDate parameters daily. 我需要能够每天自动更改fromDatetoDate参数。 I was thinking to create a powershell script and use: Get-Date -format "yyyy-MM-dd" as a variable, but not sure this is the best approach? 我当时正在考虑创建一个Powershell脚本并使用: Get-Date -format "yyyy-MM-dd"作为变量,但是不确定这是最好的方法吗? My JSON file (Oracle.json) looks like this: 我的JSON文件(Oracle.json)如下所示:

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

I am then using curl to make the POST request and output the data to flat file: 然后,我使用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

What would be the best way to make the dates dynamic so I can have the script run daily and pull from that day without having to change the dates manually? 使日期动态化的最佳方法是什么,这样我就可以每天运行脚本并从那天开始提取脚本而不必手动更改日期?

I'd use a here string and directly insert the date: 我将使用here字符串并直接插入日期:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM