我使用以下代码来获取 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 的组?
提前致谢