Get latest dates from an array per object group in Powershell

左心房为你撑大大i 提交于 2019-12-13 14:12:52

问题


I have an array $data :

PS> $data
Datum                  User                   Computer                                      
-----                  ---------              --------                                      
10/06/2013 13:10:56    geb1@TESTDOMAIN.COM    DC1$                                          
10/06/2013 13:09:25    geb2@TESTDOMAIN.COM    DC2$                                          
10/06/2013 10:05:13    geb2@TESTDOMAIN.COM    DC2$                                          
7/06/2013 16:32:47     geb1@TESTDOMAIN.COM    DC1$    

I want to get the latest dates from the $data array for each computer like this:

PS> $result
Datum                  User                   Computer                                      
-----                  ---------              --------                                      
10/06/2013 13:10:56    geb1@TESTDOMAIN.COM    DC1$                                          
10/06/2013 13:09:25    geb2@TESTDOMAIN.COM    DC2$                                          

I really couldn't get this result. Could you please help me on this?


回答1:


$data | Foreach-Object {$_.Datum = [DateTime]$_.Datum; $_} | 
Group-Object Computer | 
Foreach-Object {$_.Group | Sort-Object Datum | Select-Object -Last 1}



回答2:


Maybe you use the $GET_[]; statement?




回答3:


This should work, but I can't test it now:

$result =$data | sort computer,{ [datetime]$_.datum } -desc | group computer | % { $_.group[0] }


来源:https://stackoverflow.com/questions/17025450/get-latest-dates-from-an-array-per-object-group-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!