Advertisement
upz

Find ADusers Grp

upz
Mar 10th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #------------------------------+
  2. # Build data foundation        |
  3. # should only include relevant |
  4. # OU's, to speed up process    |
  5. #------------------------------+
  6.  
  7. $groups = Get-ADGroup -Filter *
  8. $GrpObjects = @()
  9.  
  10. foreach ($grp in $groups)
  11. {
  12.     $pro = @{
  13.     'Group' = $grp.Name
  14.     'Members' = Get-ADGroupMember -Identity $grp.Name
  15.     }
  16.  
  17.     $obj = New-Object -TypeName Psobject -Property $pro
  18.     $GrpObjects += $obj
  19. }
  20.  
  21.  
  22. #--------------------------------------------------+
  23. # This function find the grps a users is member of |
  24. # possible to expand this to include more stuff    |
  25. #--------------------------------------------------+
  26.  
  27. function find-users-grp
  28. {
  29. PARAM
  30. (
  31.     [string[]]$SamAccountName
  32. )
  33.     $check = @()
  34.     foreach ($user in $SamAccountName)
  35.     {
  36.         try
  37.         {
  38.             $check += Get-ADUser -Identity $user -ErrorAction stop  
  39.         } catch{}
  40.     }
  41.  
  42.     if ($check.Count -eq 0)
  43.     {
  44.         Write-Host -f Red "Users doesnt exist in AD"
  45.         break;
  46.     }
  47.  
  48.     foreach ($users in $SamAccountName)
  49.     {
  50.         $out = $GrpObjects | where {$_.Members.SamAccountName -eq $users}
  51.         Write-Host -ForegroundColor Yellow "$users is member of"
  52.        
  53.         foreach ($item in $out)
  54.         {
  55.             Write-Host $item.Group
  56.         }
  57.        
  58.         Write-Host ""
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement