从Specified Group获取所有AD用户

问题描述 投票:1回答:1

我正在努力阅读有史以来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

我怎么处理这个?

c# active-directory
1个回答
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);
                    }

                }
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.