我已经在这个网站上问过一个问题,只找到最近的保修结束日期,我还想使用一个表:Servicetag |EndDate 并使用 Epoch 转换保修结束日期以获得最近的日期。更准确地说:
第一步:包含序列号的列和包含保修结束日期的另一列(在表格中)。第二步:一栏带有序列号,另一栏带有保修结束日期,感谢 Epoch。第三,提取保修结束日期最高的序列号(纪元号)。第四步:用序列号格式化一列,用最高保修结束日期格式化另一列,然后将其导出到另一个 CSV 文件中。在我的 CSV 我有这个:
"Service Tag", "Start Date", "End Date"...
"57D2D85", "12/11/2015 01:00:00 ", "12/12/2016 12:59:59 "
"57D2D85", "12/11/2015 01:00:00 ", "12/12/2019 12:59:59 "
"57D2D85", "12/11/2015 01:00:00 ", "12/12/2017 12:59:59 "
"4ZRMD85", "01/12/2016 01:00:00 ", "01/13/2017 12:59:59 "
"4ZRMD85", "01/12/2016 01:00:00 ", "01/13/2018 12:59:59 "
"4ZRMD85", "01/12/2016 01:00:00 ", "01/13/2020 12:59:59 "
我希望有这样的结果:
"57D2D85", "12/11/2015 01:00:00 ", "12/12/2019 12:59:59 "
"4ZRMD85", "01/12/2016 01:00:00 ", "01/13/2020 12:59:59 "
谢谢您的帮助。
这是我的版本。[咧嘴笑] 第 1 部分伪造 CSV 文件中的读数。Import-Csv
在准备好使用真实数据时使用cmdlet。评论似乎涵盖了正在发生的事情。如果我不清楚enuf,请要求澄清...
#region >>> fake reading in the data
# in real life, use Import-CSV
$InStuff = @'
"Service Tag", "Start Date", "End Date"
"57D2D85", "12/11/2015 01:00:00 ", "12/12/2016 12:59:59 "
"57D2D85", "12/11/2015 01:00:00 ", "12/12/2019 12:59:59 "
"57D2D85", "12/11/2015 01:00:00 ", "12/12/2017 12:59:59 "
"4ZRMD85", "01/12/2016 01:00:00 ", "01/13/2017 12:59:59 "
"4ZRMD85", "01/12/2016 01:00:00 ", "01/13/2018 12:59:59 "
"4ZRMD85", "01/12/2016 01:00:00 ", "01/13/2020 12:59:59 "
'@ | ConvertFrom-Csv
#endregion >>> fake reading in the data
$Results = $InStuff |
# sort by the "End Date" property
# this would be MUCH more direct without the trailing spaces in the data
# it would also be simpler without the silly embedded spaces in the column/property names
Sort-Object {[datetime]::ParseExact($_.'End Date'.Trim(), 'MM/dd/yyyy HH:mm:ss', $Null)} |
Group-Object -Property 'Service Tag' |
ForEach-Object {
# this grabs the last item in each group
# combined with the earlier "Sort-Object" it gives the newest item for each "Service Tag"
$_.Group[-1]
}
# on screen
$Results
# send to CSV
$Results |
Export-Csv -LiteralPath "$env:TEMP\Smarty13_ExpirationDateReport.csv" -NoTypeInformation
在屏幕上 ...
Service Tag Start Date End Date
----------- ---------- --------
57D2D85 12/11/2015 01:00:00 12/12/2019 12:59:59
4ZRMD85 01/12/2016 01:00:00 01/13/2020 12:59:59
csv文件内容...
"Service Tag","Start Date","End Date"
"57D2D85","12/11/2015 01:00:00 ","12/12/2019 12:59:59 "
"4ZRMD85","01/12/2016 01:00:00 ","01/13/2020 12:59:59 "
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句