6

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?

Korki Korkig
  • 2,736
  • 9
  • 34
  • 51

3 Answers3

12
$data | Foreach-Object {$_.Datum = [DateTime]$_.Datum; $_} | 
Group-Object Computer | 
Foreach-Object {$_.Group | Sort-Object Datum | Select-Object -Last 1}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
1

Maybe you use the $GET_[]; statement?

  • I also think its way easier that you put youre data in a databse because if you kept on putting it in a array the site will be much slower –  Jun 10 '13 at 14:08
  • I think you're talking about php but it's powershell. thanks anyway. – Korki Korkig Jun 10 '13 at 14:20
1

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

$result =$data | sort computer,{ [datetime]$_.datum } -desc | group computer | % { $_.group[0] }
CB.
  • 58,865
  • 9
  • 159
  • 159