вот моя версия. [ ухмылка ] 1-й раздел подделывает чтение в CSV-файле. используйте командлет Import-Csv
, когда будете готовы использовать реальные данные. комментарии, кажется, охватывают то, что происходит. пожалуйста, попросите разъяснений, если я не был уверен 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 "