【发布时间】:2021-10-06 07:36:33
【问题描述】:
{
"logs": [
{
"timestamp": "20181216T14:36:12",
"description": "IP connectivity via interface ipmp1 has become degraded.",
"type": "alert",
"uuid": "1234567",
"severity": "Minor"
},
{
"timestamp": "20181216T14:38:16",
"description": "Network connectivity via port ibp4 has been established.",
"type": "alert",
"uuid": "12345678",
"severity": "Minor"
}
]
}
我有这个 JSON 对象,我想遍历每个对象并将时间戳更新为更易读的日期。现在,我有
$currentLogs.logs |
Where{$_.type -eq 'alert'} |
ForEach{$_.timestamp = {[datetime]::parseexact($_.timestamp, 'yyyyMMdd\THH:mm:ss', $null)}}
但是当我读取对象 $currentLogs 时,它仍然没有更新。
【问题讨论】:
-
您将
$_.timestamp设置为一个脚本块并且从未执行该脚本块。另外,除非您希望将默认格式应用于您的值的数据类型,否则您仅尝试解析该值并没有在解析后提供新格式。 -
删除
=之后的{ }大括号,围绕parseexact()行中的parseexact()命令 -
什么是“更易读的日期”格式?
-
@Theo 希望它显示 'yyyy-MM-dd HH:mm:ss'
标签: arrays json powershell foreach convertto-json