Powershell - 按 EndDate 筛选 EPOCH 方法

Powershell - EPOCH method filter by EndDate

我已经在这个网站上问了一个问题,只查找最近的保修结束日期,我还想使用 table with : Servicetag |EndDate 并将带有 Epoch 的保修结束日期转换为获取最近的日期。 更准确地说:
第一步:一列包含序列号,另一列包含保修结束日期(在 table 中)。 第二步:由于 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 "

感谢您的帮助。

这是一种简单的方法:

$CleanedWarranties = Import-Csv .\warranty.csv | Sort-Object -Property @{Expression={[DateTime]$_."End Date"}; Ascending = $True} | Group-Object -Property "Service Tag" | foreach {($_).Group[-1]}
$CleanedWarranties | Export-Csv -Path cleanedwarranties.csv -NoTypeInformation
(Get-Content .\cleanedwarranties.csv | Select-Object -Skip 1) | Out-File .\headerlesswarranties.csv -Encoding ASCII

这会导入数据,按 "End Date" 属性 对所有数据进行排序,然后根据 "Service Tag" 将它们分组,然后使用 foreach 到 select 只有每个组的最后一个元素。然后你可以使用 Select-Object -Skip 1.

摆脱 header

这是我的版本。 [grin] 第 1 部分假装读取 CSV 文件。准备好使用真实数据时使用 Import-Csv cmdlet。这些评论似乎涵盖了正在发生的事情。如果我不清楚,请要求澄清......

#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 "