列出所有AD和组织单位组

时间:2013-07-15 16:41:28

标签: c# active-directory windows-server

我使用以下代码获取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());
        }));
    }

代码运行良好,但不返回组织单位中声明的组... 如何获得所有组,包括那些进入OU的组?

提前致谢

0 个答案:

没有答案