简体   繁体   中英

Get latest dates from an array per object group in Powershell

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?

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

Maybe you use the $GET_[]; statement?

这应该有效,但我现在无法测试它:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM