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