2010-01-20 5 views

Répondre

14

D'abord, trouvez le groupe. Puis énumérer ses utilisateurs en utilisant GetMembers().

using (var context = new PrincipalContext(ContextType.Domain)) 
{ 
    using (var group = GroupPrincipal.FindByIdentity(context, "groupname")) 
    { 
      var users = group.GetMembers(true); // recursively enumerate 
      ... 
    } 
} 

Notez qu'il ya un bug, fixé dans .NET 4.0, où il ne parviendra pas à énumérer plus de 1500 membres du groupe. Si vous avez un grand groupe, vous devez utiliser un alternative method en utilisant les méthodes plus anciennes dans System.DirectoryServices.

4

Consultez cet article Managing Directory Security Principals in the .NET Framework 3.5 pour un excellent aperçu de ce que vous pouvez faire avec System.DirectoryServices.AccountManagement dans .NET 3.5.

En ce qui concerne la récupération des membres d'un groupe, vous faites ceci:

// build the principal context - use the NetBIOS domain name 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAIN"); 

// get the group you're interested in 
GroupPrincipal group = GroupPrincipal.FindByIdentity("cn=YourGroupname"); 

// iterate over its members 
foreach(Principal p in group.Members) 
{ 
    // do whatever you need to do to its members here    
} 

Hope this helps!

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.DirectoryServices.AccountManagement; 

namespace ExportActiveDirectoryGroupsUsers 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      if (args == null) 
      { 
       Console.WriteLine("args is null, useage: ExportActiveDirectoryGroupsUsers OutputPath"); // Check for null array 
      } 
      else 
      { 
       Console.Write("args length is "); 
       Console.WriteLine(args.Length); // Write array length 
       for (int i = 0; i < args.Length; i++) // Loop through array 
       { 
        string argument = args[i]; 
        Console.Write("args index "); 
        Console.Write(i); // Write index 
        Console.Write(" is ["); 
        Console.Write(argument); // Write string 
        Console.WriteLine("]"); 
       } 
       try 
       { 
        using (var ServerContext = new PrincipalContext(ContextType.Domain, ServerAddress, Username, Password)) 
        { 
         /// define a "query-by-example" principal - here, we search for a GroupPrincipal 
         GroupPrincipal qbeGroup = new GroupPrincipal(ServerContext, args[0]); 

         // create your principal searcher passing in the QBE principal  
         PrincipalSearcher srch = new PrincipalSearcher(qbeGroup); 

         // find all matches 
         foreach (var found in srch.FindAll()) 
         { 
          GroupPrincipal foundGroup = found as GroupPrincipal; 

          if (foundGroup != null) 
          { 
           // iterate over members 
           foreach (Principal p in foundGroup.GetMembers()) 
           { 
            Console.WriteLine("{0}|{1}", foundGroup.Name, p.DisplayName); 
            // do whatever you need to do to those members 
           } 
          } 

         } 
        } 
        //Console.WriteLine("end"); 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine("Something wrong happened in the AD Query module: " + ex.ToString()); 
       } 
       Console.ReadLine(); 
      } 
     } 
    } 
} 
Questions connexes