从Specified Group获取所有AD用户

时间:2018-02-28 11:26:15

标签: c# active-directory

我正在尝试阅读AD集团的所有成员。我得到了包含以下代码的组

PrincipalContext yourOU = new PrincipalContext(ContextType.Domain, "test.domain.CH", "OU=Verteiler,OU=Gruppen,OU=SGV,OU=Gruppe_04,OU=Kunden,DC=test,DC=domain,DC=CH");
GroupPrincipal findAllGroups = new GroupPrincipal(yourOU, "*");
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups);
foreach (var group in ps.FindAll())
{
    Console.WriteLine(group.DisplayName);                    
}

现在我正在尝试向每个小组展示每个用户。

enter image description here

我该如何处理?

1 个答案:

答案 0 :(得分:0)

我自己尝试过,请在您的foreach中调用以下代码并发送您的Groupname

 static async void populateGroups(string ADGroupName)
    {
        // set up domain context
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

        // find the group in question
        GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, ADGroupName);

        // if found....
        if (group != null)
        {
            // iterate over members
            foreach (Principal p in group.GetMembers())
            {
               // Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);

                // do whatever you need to do to those members
                UserPrincipal theUser = p as UserPrincipal;



                if (theUser != null)
                {
                    if (theUser.IsAccountLockedOut())
                    {
                        Console.WriteLine("The user: {0} is member of following Group {1}", theUser, ADGroupName);
                    }

                    else
                    {
                        Console.WriteLine("The user: {0} is member of following Group {1}", theUser, ADGroupName);
                    }

                }
            }
        }
    }