使用 PowerShell 解析和操作 Json 文件中的日期
Parsing and manipulating Dates in a Json file using PowerShell
我需要帮助解析 JSON 文件中的日期。
简而言之,我的 PowerShell 脚本将信息从 JSON 源文件提取到 CSV 文件中。我的 Json 文件有这些字段:
[
{
"Type": "Some String",
"Score": "Some String" ,
"Dateobtained": "2021-07-03T07:15:48.493",
"Status": "Some String",
"Description": "Some String"
},
{
"Type": "Some String",
"Score": "Some String" ,
"Dateobtained": "2021-06-24T07:15:48.493",
"Status": "Some String",
"Description": "Some String"
}
]
我想过滤掉旧信息。例如,今天是 6 月 7 日,我只想包含仅在今年 7 月获得的信息(Dateobtained
不应提取 7 月之前的数据)
我已经有了这个来提取其他信息:
$json = Get-Content "source.json" | ConvertFrom-Json
$json | ForEach-Object {
# Iterate through each of the tests and create object for each
$_.Findings| ForEach-Object {
[PSCustomObject]@{
'Type' = $_.Type
'Score' = $_.Score
'Status' = $_.Status
'Description' = $_.Description
}
}
} | Export-Csv -Path "out.csv" -NoTypeInformation
我不太确定如何继续解析该日期,然后将 CSV 文件上的输出限制为当前月份。
提前致谢:-)
给定以下数组:
PS /> $json | ft
Type Score Dateobtained Status Description
---- ----- ------------ ------ -----------
Some String Some String 2021-07-03T07:15:48.493 Some String Some String
Some String Some String 2021-06-24T07:15:48.493 Some String Some String
Some String Some String 2021-05-24T07:15:48.493 Some String Some String
Some String Some String 2021-04-24T07:15:48.493 Some String Some String
如果你只想过滤那些 DateObtained
在当前月份之间的对象(对不起我的英语),你可以这样做:
$thisMonth = [datetime]::Today.Month
$json | Where-Object {
([datetime]$_.dateobtained).Month -eq $thisMonth
# OR
# ($_.dateobtained -as [datetime]).Month -eq $thisMonth
} |
Select-Object * -ExcludeProperty Dateobtained |
Export-Csv -Path "out.csv" -NoTypeInformation
这只会产生一个对象(第一个):
Type Score Status Description
---- ----- ------ -----------
Some String Some String Some String Some String
注:
如果 属性 没有实际日期,$_.Dateobtained
上的 [datetime]
类型转换可能会引发错误。如果您不确定,请改用 $_.Dateobtained -as [datetime]
:
PS /> [datetime]'notadate'
Cannot convert value "notadate" to type "System.DateTime". Error: "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."
At line:1 char:1
+ [datetime]'notadate'
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider
PS /> 'notadate' -as [datetime] # // No errors here
PS />
我需要帮助解析 JSON 文件中的日期。
简而言之,我的 PowerShell 脚本将信息从 JSON 源文件提取到 CSV 文件中。我的 Json 文件有这些字段:
[
{
"Type": "Some String",
"Score": "Some String" ,
"Dateobtained": "2021-07-03T07:15:48.493",
"Status": "Some String",
"Description": "Some String"
},
{
"Type": "Some String",
"Score": "Some String" ,
"Dateobtained": "2021-06-24T07:15:48.493",
"Status": "Some String",
"Description": "Some String"
}
]
我想过滤掉旧信息。例如,今天是 6 月 7 日,我只想包含仅在今年 7 月获得的信息(Dateobtained
不应提取 7 月之前的数据)
我已经有了这个来提取其他信息:
$json = Get-Content "source.json" | ConvertFrom-Json
$json | ForEach-Object {
# Iterate through each of the tests and create object for each
$_.Findings| ForEach-Object {
[PSCustomObject]@{
'Type' = $_.Type
'Score' = $_.Score
'Status' = $_.Status
'Description' = $_.Description
}
}
} | Export-Csv -Path "out.csv" -NoTypeInformation
我不太确定如何继续解析该日期,然后将 CSV 文件上的输出限制为当前月份。
提前致谢:-)
给定以下数组:
PS /> $json | ft
Type Score Dateobtained Status Description
---- ----- ------------ ------ -----------
Some String Some String 2021-07-03T07:15:48.493 Some String Some String
Some String Some String 2021-06-24T07:15:48.493 Some String Some String
Some String Some String 2021-05-24T07:15:48.493 Some String Some String
Some String Some String 2021-04-24T07:15:48.493 Some String Some String
如果你只想过滤那些 DateObtained
在当前月份之间的对象(对不起我的英语),你可以这样做:
$thisMonth = [datetime]::Today.Month
$json | Where-Object {
([datetime]$_.dateobtained).Month -eq $thisMonth
# OR
# ($_.dateobtained -as [datetime]).Month -eq $thisMonth
} |
Select-Object * -ExcludeProperty Dateobtained |
Export-Csv -Path "out.csv" -NoTypeInformation
这只会产生一个对象(第一个):
Type Score Status Description
---- ----- ------ -----------
Some String Some String Some String Some String
注:
如果 属性 没有实际日期,$_.Dateobtained
上的 [datetime]
类型转换可能会引发错误。如果您不确定,请改用 $_.Dateobtained -as [datetime]
:
PS /> [datetime]'notadate'
Cannot convert value "notadate" to type "System.DateTime". Error: "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."
At line:1 char:1
+ [datetime]'notadate'
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider
PS /> 'notadate' -as [datetime] # // No errors here
PS />