Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get all users from a group in Active Directory

I'm trying to get all users of a particular group in AD, then return a list of Employees as mapped to properties in my Employee class. I have:

My Filter is producing no results - what should it be?

Also, I tried the first solution here: List of users in specific Active Directory Distribution Group, but I need details such as mobile, extension, etc. which I couldn't get with that method.

public static List<Employee> CreateEmployeeList(string department) {     List<Employee> employees = new List<Employee>();     string filter = string.Format("(&(ObjectClass=person)(memberOf=CN={0},OU=Users & Groups,OU=Blah,DC=Blah,DC=Blah,DC=Blah))", department);      DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);     DirectorySearcher searcher = new DirectorySearcher(adRoot);     searcher.SearchScope = SearchScope.Subtree;     searcher.ReferralChasing = ReferralChasingOption.All;     searcher.Filter = filter;     SearchResultCollection results = searcher.FindAll();      foreach (SearchResult user in results)     {         // do whatever you need to do with the entry          if (user != null)         {             UserDirectoryEntry = user.GetDirectoryEntry();             string displayName = GetUserProperty("displayName");             string firstName = GetUserProperty("givenName");             string lastName = GetUserProperty("sn");             string email = GetUserProperty("mail");             string tel = GetUserProperty("telephonenumber");             string extension = GetUserProperty("ipphone");             string mobile = GetUserProperty("mobile");             string title = GetUserProperty("description");             employees.Add(new Employee{ FullName = displayName, FirstName = firstName, Surname = lastName, Email = email.ToLower(), Telephone = tel, Extension = extension, Mobile = mobile, JobTitle = title });         }     }     return employees; } 
like image 332
raklos Avatar asked Oct 27 '11 11:10

raklos


People also ask

How do I get a list of users in Active Directory groups?

Use Get-ADGroupMember cmdlet to List Members of an Active Directory Group. The PowerShell Get-ADGroupMember cmdlet is used to list the members of an Active Directory group. You can just type the cmdlet in a PowerShell window and you'll be prompted to enter the name of the group you want to use.

How do I Export a list of users from ad group?

Export AD group members to CSVRun PowerShell as administrator. Change the path to the scripts folder. Run the PowerShell script to export AD group members to CSV file. Wait till it completes.

How do I get ad Group Details in PowerShell?

To find AD groups with PowerShell, you can use the Get-ADGroup cmdlet. With no parameters, Get-ADGroup will query AD and return all groups in a domain using the Filter parameter. The Filter parameter is required. It exists to limit the groups returned based on various criteria.


1 Answers

using (var context = new PrincipalContext(ContextType.Domain, "domainName")) {     using (var group = GroupPrincipal.FindByIdentity(context, "groupName"))     {         if (group == null)         {             MessageBox.Show("Group does not exist");         }         else         {             var users = group.GetMembers(true);             foreach (UserPrincipal user in users)             {                  //user variable has the details about the user              }         }      } } 
like image 66
Dalton Avatar answered Sep 20 '22 04:09

Dalton