简体   繁体   中英

Powershell: Comparing multiple values in an array

I've got another tricky problem with Powershell. I have a list of ~4000 entries where I have the following details:

<Computername> | <Username> | <StartDate> | <LastDate> | <IDNum>
computer1      | jsmith     | 4/12/12     | 4/12/13    | 52648
computer1      | jsmith     | 4/12/12     | -          | 52649
computer1      | jsmith     | 4/12/12     | 8/7/13     | 52644
computer2      | hreid      | 2/5/12      | 8/5/13     | 52396
computer2      | hreid      | 4/10/12     | 5/18/13    | 52300
computer3      | mdrake     | 2/10/11     | 12/18/12   | 52590
computer3      | mdrake     | 5/10/11     | 3/20/12    | 52593

Its a CSV, I'm trying to find a way to compare the Computername and the LastDate field and spit out a list of the most recent line with the associated computername value. I'm able to read the CSV into a variable, but I'm having a hell of a time with get-member values and for each loops.

Ideally the output would look like this:

<Computername> | <Username> | <StartDate> | <LastDate> | <IDNum>
computer1      | jsmith     | 4/12/12     | 8/7/13     | 52644
computer2      | hreid      | 2/5/12      | 8/5/13     | 52396
computer3      | mdrake     | 5/10/11     | 3/20/12    | 52593

Kind of complex... I'm really not able to break this down in my head. Any help? Or even ideas on how I can start scripting this... I'm hitting a wall here.

--Update--

Thank you Rynant. So i found out that apparently my source list is a little more complex. Its missing some computer names from the list... in addition its missing some lastdate entries also. So I need to alter the original list to look more like this:

<Computername> | <Username> | <StartDate> | <LastDate> | <IDNum>
computer1      | jsmith     | 4/12/12     | 4/12/13    | 52648
computer1      | jsmith     | 4/12/12     | -          | 52649
computer1      | jsmith     | 4/12/12     | 8/7/13     | 52644
computer2      | hreid      | 2/5/12      | 8/5/13     | 52396
computer2      | hreid      | 4/10/12     | 5/18/13    | 52300
computer3      | mdrake     | 2/10/11     | 12/18/12   | 52590
computer3      | mdrake     | 5/10/11     | 3/20/12    | 52593
               | fmann      | 4/11/12     | 3/20/13    | 52342

I'll try with your code example and see how I do. I'm really annoyed at this source list as I'm finding all kinds of things missing for some entries... argh.

You could group by Computername, then for each group, sort by LastDate and select the last item.

You will have to adjust to meet your formatting needs, but it would look something like this.

Import-Csv -Path .\computers.csv|
    group -Property Computername|
    foreach {
        $_.group|
            sort { try { [datetime]$_.LastDate } catch {} }|
            select -last 1
    }|
    Export-Csv -Path .\computers_LastDate.csv -NoTypeInformation

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