finding matching values in two columns

This topic contains 3 replies, has 2 voices, and was last updated by  Don Jones 2 weeks, 3 days ago.

  • Author
    Posts
  • #83566

    John Curtiss
    Participant

    maybe not the best title, and this is probably a logic question more than a powershell question. but here we are. hopefully i'm missing something simple. I have a two column array. column 1 "SERVER" is full of server names, and column 2 "MEMBER" is each group in the Administrators group on that server. So column 1 has duplicates depending on how many groups are in its Administrators group.

    SERVER MEMBER
    server1 domain admins
    server1 some-group
    server1 server1-localadmins
    server2 domain admins
    server2 some-other-group
    server2 server2-localadmins
    server3 domain admins
    server4 server4-localadmins

    I want to make sure that every server in column 1 has the "[servername]-localadmins" group in column 2. so in the samp;le above, I want to go through that list and return server3 because there is no "server3-localadmins" in column 2.

  • #83570

    Don Jones
    Keymaster

    I'd probably run the whole collection through Select-Object -unique to generate and store a list of unique server names.

    I'd then enumerate those. For each server name, I'd select – from the original collection and using Where-Object – all objects having the current server name. I'd then further Where-Object that to get just the objects with localadmins, and send that to Measure-Object.

    If the resulting Measure object had a count of zero, I'd output the current server name.

    Vaguely, because I haven't had coffee yet...

    $coll = do whatever you do to populate the collection
    
    $names = $coll | select-object -prop server -unique | select -expand server
    
    ForEach ($name in $names) {
      $count = $coll | Where server -eq $name | Where member -eq localadmins | measure
      If ($count.count -eq 0) {
       Write $name
      }
    }
    
    
  • #83579

    John Curtiss
    Participant

    awesome. thank you. I thought about filtering down a unique list of server names, but didn't know how.

    can I also ask about the "select -expand server"? i assume that is why you don't have to put $name.server in the where clause. "expand" just removes the column heading from the array, more or less?

  • #83582

    Don Jones
    Keymaster

    Well, more accurately, it “expands” (or “extracts”) the contents of the property into a standalone string. But yeah, it's so I don't have to refer to a property of an object.

You must be logged in to reply to this topic.