Get Groups From OU using DirectoryServices.AccountManagement

后端 未结 1 379
感动是毒
感动是毒 2020-12-28 18:24

I\'d like to use AccountManagement to list all the groups in an Organizational Unit.

The following snippet works with DirectoryServices but I would have to instanci

相关标签:
1条回答
  • 2020-12-28 19:08

    You can set the PrincipalContext to the OU where you want to start the search and use the PrincipalSearcher-class in System.DirectoryService.AccountManagement to accomplish what you need, like this:

    PrincipalContext yourOU = new PrincipalContext(ContextType.Domain, "mycompany.local", "OU=Marketing,OU=Operations,OU=Applications,DC=mycompany,DC=local");
    GroupPrincipal findAllGroups = new GroupPrincipal(yourOU, "*");
    PrincipalSearcher ps = new PrincipalSearcher(findAllGroups);
    foreach(var group in ps.FindAll())
    {
      Console.WriteLine(group.DistinguishedName);
    }
    Console.ReadLine();
    
    0 讨论(0)
提交回复
热议问题