【发布时间】:2018-05-25 03:21:19
【问题描述】:
我有一个包含以下内容的 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 替换它吗?
【问题讨论】:
-
您可以使用
(Get-Content yourfile) - replace '(?<="LogDirectoryPath": "C:\\\\log\\\\)(?<date>[\d-]+)(?=",)', (Get-Date -Format "yyyy-MM-dd") | Set-Content yourfile创建计划任务,但您应该考虑使用像 TimestampFormat 这样的格式。每天更改 JSON 文件是一种代码味道。
标签: powershell replace