【问题标题】:Powershell - EPOCH method filter by EndDatePowershell - EndDate 的 EPOCH 方法过滤器
【发布时间】:2020-05-21 02:04:51
【问题描述】:

我已经在本网站上提出了一个问题,仅查找最近的保修结束日期,我还想使用带有 : 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 "

感谢您的帮助。

【问题讨论】:

  • 感谢 Drake perera 的布局

标签: powershell filter epoch


【解决方案1】:

这是一个简单的方法:

$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

这会导入数据,按“结束日期”属性对其进行排序,根据“服务标签”将它们分组,然后使用foreach 仅选择每个组的最后一个元素。然后您可以使用Select-Object -Skip 1 摆脱标题。

【讨论】:

  • 整洁! [grin] 我唯一要改变的是... [1] 测试[datetime] 调用以确保它可以使用dd/MM/yyyy 作为语言环境格式。我不得不处理欧盟/美国/军事时间,当我得到一个而期待另一个时,我发现了许多故障。 [2] 将第一行包裹起来,这样就可以在不横向滚动的情况下阅读它。 [3] 在尝试解析它们之前,我可能还会删除示例日期中的尾随空格。
  • 你好,它似乎在创造奇迹。感谢您的回复和研究。
  • 我在导出 .csv 时有一个问题,我们可以删除 .csv 中的“servicetag”标签吗?目标是只有序列号而不是服务标签。你知道怎么做吗?
  • @Smarty13 更新!
【解决方案2】:

这是我的版本。 [grin] 第 1 部分假装读取 CSV 文件。准备好使用真实数据时使用Import-Csv cmdlet。 cmets 似乎涵盖了正在发生的事情。如果我不清楚 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 "

【讨论】:

  • Lee_Dailey,首先感谢您的回复。我首先测试了你的版本,它运行良好,但是正如你在现实生活中所说的,我必须说:使用 Import-Csv。只有我标记: $Instuff = Import-csv C:\... .csv 但它不起作用。你能告诉我为什么吗?谢谢你。 (我在评论该地区>>数据中的虚假阅读)
  • @Smarty13 - 你应该能够[A]注释掉或删除#region/#endregion的东西然后[B]添加$InStuff = Import-Csv c:\Full\Path\To\Your\FIle.csv,它应该可以工作。 ///// 如果没有,那么您看到了什么错误?在您的情况下,“不起作用”是什么意思?
  • 我在导出 .csv 时有一个问题,我们可以删除 .csv 中的“servicetag”标签吗?目标是只有序列号而不是服务标签。你知道怎么做吗?
  • @Smarty13 - 不客气! [grin] ///// 重新排列对象中的属性的常用方法是使用Select-Object 给你想要的道具,或者使用[PSCustomObject] 来做同样的事情结构更整洁、速度更快。
猜你喜欢
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-03
  • 2023-03-26
  • 2021-11-10
  • 1970-01-01
相关资源
最近更新 更多