Welcome Forums General PowerShell Q&A Validate CSV of SamAccountNames against multiple groups

Viewing 1 reply thread
  • Author
    Posts
    • #168337
      cm
      Participant
      Topics: 11
      Replies: 27
      Points: 34
      Rank: Member

      Hello,

      I need to validate a CSV of SANs against multiple group. I only want to return the users who do not belong to ALL of the groups. Currently my code returns everyone and I understand why (it’s only looking at the members of the last group), but not how to fix. I also realize I probably need to create a hashtable for my output to remove the “@{SamAccountName=” in my results. List of CSV may contain 3000 accts if it helps.

      EDIT: By not being part of these groups we determine they are no longer in AD, that’s why I’m not using memberOf.

      $Users = Import-Csv -Path "C:\AdminTools\PowerShell\SamAccountNames.csv"
      
      $agroup = "group1"
      
      $bgroup = "group2"
      
      $cgroup = "group3"
      
      $dgroup = "group4"
      
      $egroup = "group5"
      
      $fgroup = "group6"
      
      $Members = Get-ADGroupMember -Identity "$agroup" -Recursive | Select -ExpandProperty samaccountname
      
      $Members = Get-ADGroupMember -Identity "$bgroup" -Recursive | Select -ExpandProperty samaccountname
      
      $Members = Get-ADGroupMember -Identity "$cgroup" -Recursive | Select -ExpandProperty samaccountname
      
      $Members = Get-ADGroupMember -Identity "$dgroup" -Recursive | Select -ExpandProperty samaccountname
      
      $Members = Get-ADGroupMember -Identity "$egroup" -Recursive | Select -ExpandProperty samaccountname
      
      $Members = Get-ADGroupMember -Identity "$fgroup" -Recursive | Select -ExpandProperty samaccountname
      
      foreach ($user in $users)
      
      {
      
      if ($Members -Contains $User.SamAccountName){
      
      #Write-Host "$User in Group"
      
      } Else {
      
      Write-Host "$User not in group"
      
      }
      
      }

       

    • #168421
      cm
      Participant
      Topics: 11
      Replies: 27
      Points: 34
      Rank: Member

      Seems to be working now. I forgot to use += over = so it doesnt overwrite.

Viewing 1 reply thread
  • The topic ‘Validate CSV of SamAccountNames against multiple groups’ is closed to new replies.