0

I using the following code to get the groups of ActiveDirectory.

public static Task<List<String>> GetGroups(String domain = "")
    {
        return (Task.Factory.StartNew<List<String>>(() =>
        {
            List<String> list = new List<String>();
            try
            {
                if (String.IsNullOrWhiteSpace(domain))
                {
                    SelectQuery sQuery = new SelectQuery("Win32_Group", "LocalAccount=\"true\"");
                    ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
                    list.AddRange(mSearcher.Get().Cast<ManagementObject>().Select(x => x["Name"].ToString()));
                }
                else
                {
                    DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://" + domain);

                    DirectorySearcher searcher = new DirectorySearcher(directoryEntry, "(&(objectClass=group))");
                    searcher.SearchScope = SearchScope.Subtree;
                    list.AddRange(searcher.FindAll().Cast<SearchResult>().Select(x => x.GetDirectoryEntry().Name.Replace("CN=", String.Empty)));
                }
            }
            catch (COMException e)
            {
                Logger.WriteException(e);
            }
            return (list.OrderBy(x => x).ToList());
        }));
    }

The code works well but dit not return the groups that are declared in an Organizational Unit... How can I get all group including those that are into OU ?

Thanks in advance

Geotinc
  • 289
  • 1
  • 4
  • 21
  • Might consider using PrincipleContext under System.DirectoryService.AccountManagement. See this link here http://stackoverflow.com/questions/1927261/get-groups-from-ou-using-directoryservices-accountmanagement – Bearcat9425 Jul 15 '13 at 16:57
  • How many groups are stored in your Active Directory? – Hans Jul 15 '13 at 17:07
  • It is really difficult to help you if you does not answer the questions in the comments section. – Hans Jul 16 '13 at 16:21

0 Answers0