Active Directory 内のすべてのグループを取得するには、このコードを C# で記述しました。サーバー名、OU、DCなどを渡す必要がないため、完全に機能します。
UserPrincipal current_user = UserPrincipal.Current;
PrincipalContext current_context = current_user.Context;
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal qbeUser = new GroupPrincipal(ctx);
Principal userOrGroup = qbeUser as Principal;
userOrGroup.Name = "*";
PrincipalSearcher searcher = new PrincipalSearcher(userOrGroup);
List<string> AllGroups = new List<string>();
// enumerate the results - you need to check what kind of principal you get back
foreach (Principal found in searcher.FindAll())
{
// is it a UserPrincipal - do what you need to do with that...
if (found is UserPrincipal)
{
// ......
}
else if (found is GroupPrincipal)
{
AllGroups.Add(found.Name);
//GroupPrincipal gp = found as GroupPrincipal;
//var data = gp.GetMembers();
// if it's a group - do whatever you need to do with a group....
}
}
//return AllGroups;
問題は、必要のないグループが多すぎることです
PerformanceLogUsers、SchemaAdmins、HelpServiceGroups、Telnet クライアントなど。
管理者、ゲスト、その他のユーザー作成グループなどのグループのみが必要です。これらは特別なグループなどであるなどについて読んだことがあります。
この点でどんな助けでも大歓迎です。