2014-11-17 2 views
6

인 Active Directory 그룹 찾기 특정 이름으로 시작하는 그룹 이름을 가진 모든 Active Directory 그룹을 반환하는 C# 스크립트를 작성해야합니다. 다음 코드를 사용하여 하나의 그룹을 반환 할 수 있습니다.그룹 이름이

PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, "Groupname"); 

그러나 그룹 이름이 시작되는 모든 그룹 (예 : "GroupPrefix")을 원합니다. 다음 코드를 사용하여 이러한 모든 그룹을 탐색하고 "구성원"을 나중에 검색을 위해 사용할 수있는 배열/목록에 저장하려고합니다.

foreach (UserPrincipal p in grp.GetMembers(true)) 

나는 이것으로 얻을 수있는 도움을 많이 주셔서 감사합니다.

답변

6

당신은 PrincipalSearcher 사용할 수 있으며, "쿼리별로 예를 들어"교장은 검색을 수행 할 :

// create your domain context 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // define a "query-by-example" principal - here, we search for a GroupPrincipal 
    // and with the name like some pattern 
    GroupPrincipal qbeGroup = new GroupPrincipal(ctx); 
    qbeGroup.Name = "GroupPrefix*"; 

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

    // find all matches 
    foreach(var found in srch.FindAll()) 
    { 
     // do whatever here - "found" is of type "Principal" 
    } 
} 

당신은 이미하지 않은 경우 - 절대적으로 만드는 방법을 잘 보여주는 MSDN 문서 Managing Directory Security Principals in the .NET Framework 3.5 읽기 System.DirectoryServices.AccountManagement의 새로운 기능을 최대한 활용하십시오. 또는 MSDN documentation on the System.DirectoryServices.AccountManagement 네임 스페이스를 참조하십시오.

  • DisplayName (일반적으로 : 이름 + 공간 + 성은 물론

    , 필요에 따라, 당신은 당신이 만드는 것이 "쿼리별로 예를 들어"그룹 교장에 다른 속성을 지정할 수 있습니다)

  • SAM Account Name - 윈도우/AD 계정 이름이

당신은 GroupPrincipal의 특성 중 하나를 지정하고 같은 사람들을 사용할 수 있습니다 "쿼리별로 예를 들어"당신의 PrincipalSearcher합니다.

+1

잡지 링크가 작동하지 않습니다. – TernaryTopiary

+0

예 첫 번째 링크가 작동하지 않습니다. –