Array match

Welcome Forums General PowerShell Q&A Array match

Viewing 4 reply threads
  • Author
    Posts
    • #208728
      Participant
      Topics: 3
      Replies: 7
      Points: 38
      Rank: Member

      Dear all, I need your help with comparing two arrays.

      I have two lists:

      File1.csv:
      “Group”,”Name”,”SamAccountName”,”distinguishedName”
      “Vpn Users”,”XXXXXX”,”XXXXXX”,”CN=XXXXXX,OU=example,DC=mydomain,DC=world,DC=com”
      “Vpn Users”,”YYYYYY”,”YYYYYY”,”CN=YYYYYY,OU=example,DC=mydomain,DC=world,DC=com”
      “Vpn Users”,”ZZZZZZ”,”ZZZZZZ”,”CN=ZZZZZZ,OU=example,DC=mydomain,DC=world,DC=com”

      File2.csv:
      “Name”,”Surname”,”Login”,”Type”
      “Name1″,”Surname1″,”XXXXXX”,”Employee”
      “Name2″,”Surname2″,”YYYYYY”,”Consultant”

      I need to match all entry in File1 with all entry in File2 and generate output like

      User XXXXXX is Employee
      User YYYYYY is Consultant
      User ZZZZZZ is not present

      Could someone please help me kindly?
      It would help me a lot.

      Thanks in advance.
      Andrea.
      Ciao

    • #208740
      Participant
      Topics: 4
      Replies: 2248
      Points: 5,490
      Helping Hand
      Rank: Community MVP

      Please, when you post code or example data you should format this as code using the code tag button named “PRE”. Thanks.

      You could use the cmdlet Compare-Object but that would require the data to have at least one common member in each data set. At the moment I cannot see any in your example data. You have in both data sets a column named name but they don’t have any common name in it.

      If you want to compare the data based on SAMAccountName from the first data set and Login from the second data set you could do it like this:

      $Data1 = @'
      "Group","Name","SamAccountName","distinguishedName"
      "Vpn Users","XXXXXX","XXXXXX","CN=XXXXXX,OU=example,DC=mydomain,DC=world,DC=com"
      "Vpn Users","YYYYYY","YYYYYY","CN=YYYYYY,OU=example,DC=mydomain,DC=world,DC=com"
      "Vpn Users","ZZZZZZ","ZZZZZZ","CN=ZZZZZZ,OU=example,DC=mydomain,DC=world,DC=com"
      '@ |
          ConvertFrom-Csv 
      
      $Data2 = @'
      "Name","Surname","Login","Type"
      "Name1","Surname1","XXXXXX","Employee"
      "Name2","Surname2","YYYYYY","Consultant"
      '@|
          ConvertFrom-Csv |
              Select-Object -Property Name,Surname,@{Name = 'sAMAccountName';Expression = {$_.Login}},Type
      
      Compare-Object -ReferenceObject $Data2 -DifferenceObject $Data1 -Property sAMAccountName -PassThru -IncludeEqual |
          Format-Table -AutoSize
    • #208752
      Participant
      Topics: 3
      Replies: 7
      Points: 38
      Rank: Member

      Dear Olaf, thank you very much.

      Sorry! Next time I’ll use the PRE tag. I promise you 🙂

      Your code work perfectly.Thanks
      In my code the 2 arrays are populated with Import-csv command.

      How can I use your logic using the Import-csv instruction?

      Sorry…I’m very ignorant !

      Thank you so much Olaf.
      Andrea

    • #208755
      Participant
      Topics: 4
      Replies: 2248
      Points: 5,490
      Helping Hand
      Rank: Community MVP

      How can I use your logic using the Import-csv instruction?

      You simply import the data into $Data1 and $Data2.

    • #208797
      Participant
      Topics: 3
      Replies: 7
      Points: 38
      Rank: Member

      Olaf,

      thank you very very very much for your precious help!

       

      Best regards

      Andrea

Viewing 4 reply threads
  • You must be logged in to reply to this topic.