public void execute(string querySearchFilter, IEntryProcessor <UserGroup> entryProcessor) { using ( DirectoryEntry root = new DirectoryEntry("LDAP://" + _ldapConfig.GetUrl() + "/" + _ldapConfig.GetRootDn(), _ldapConfig.GetBindUser(), _ldapConfig.GetBindPassword())) using (var ctx = new PrincipalContext(ContextType.Domain, _ldapConfig.GetUrl(), _ldapConfig.GetBindUser(), _ldapConfig.GetBindPassword())) using ( //new string[] { "name" } DirectorySearcher search = new DirectorySearcher(root, querySearchFilter)) using (SearchResultCollection results = search.FindAll()) { IList <UserGroup> userGroups = new List <UserGroup>(); foreach (SearchResult sr in results) { var group = new UserGroup(sr.Properties["samaccountname"][0].ToString()); Console.WriteLine("Processing Group: " + group.Name); ResultPropertyValueCollection description = sr.Properties["description"]; if (description.Count > 0) { group.Description = description[0].ToString(); } //Create a Role by name getting the suffix from the name of the group group.Role = new Role(group.Name.Split('_').Last()); AddUsersToGroups(ctx, group); userGroups.Add(group); } entryProcessor.ProcessChanged(userGroups); } }