SHARE
TWEET

Untitled

a guest Feb 20th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getADGroups($username)
  2. {
  3.   $root = ([adsi]"").distinguishedName;
  4.  
  5.   $searcher = new-object System.DirectoryServices.DirectorySearcher($root);
  6.  
  7.   $searcher.filter = "(&(objectClass=user)(sAMAccountName=$username))";
  8.  
  9.   $user = $searcher.findall();
  10.  
  11.   $groupNames = @();
  12.  
  13.   if ($user.count -eq 1)
  14.   {
  15.     $groups = $user[0].Properties.memberof;
  16.  
  17.     [regex]$regex = "^CN=(.*?),";
  18.  
  19.     $groups | % {
  20.       $groupNames += $regex.matches($_) | % { $_.groups[1].value }
  21.       };
  22.   }
  23.   else
  24.   {
  25.       write-host "invalid username, result count:" $user.count -foregroundcolor "red";
  26.   }
  27.   $groupNames;
  28. }
  29. getADGroups $args[0]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top