2009-10-22 6 views
11

활성 디렉토리에서 모든 컴퓨터/기계/pc의 목록을 얻는 방법에 궁금한가요?활성 디렉토리에있는 모든 컴퓨터 나열

(이 페이지 검색 엔진 미끼 만들려고 노력하는 것은, 자신을 응답 할 것이다. 누군가가 더 나은 경우 위원장이 받아 들일 응답)이 매우 큰 도메인, 또는 도메인을 제한하는 방법 방법에 구성한 경우

답변

23

검색 당 많은 항목이 반환 될 수 있으므로 페이징을 사용해야 할 수도 있습니다.

using System.DirectoryServices; //add to references 

public static List<string> GetComputers() 
{ 
    List<string> ComputerNames = new List<string>(); 

    DirectoryEntry entry = new DirectoryEntry("LDAP://YourActiveDirectoryDomain.no"); 
    DirectorySearcher mySearcher = new DirectorySearcher(entry); 
    mySearcher.Filter = ("(objectClass=computer)"); 
    mySearcher.SizeLimit = int.MaxValue; 
    mySearcher.PageSize = int.MaxValue; 

    foreach(SearchResult resEnt in mySearcher.FindAll()) 
    { 
     //"CN=SGSVG007DC" 
     string ComputerName = resEnt.GetDirectoryEntry().Name; 
     if (ComputerName.StartsWith("CN=")) 
      ComputerName = ComputerName.Remove(0,"CN=".Length); 
     ComputerNames.Add(ComputerName); 
    } 

    mySearcher.Dispose(); 
    entry.Dispose(); 

    return ComputerNames; 
} 
1

(objectCategory = computer)와 같은 LDAP 쿼리가 트릭을 수행해야합니다. 제안 EKS 무엇 - 짐

5

올바른이지만, 조금 느린을 수행하고 있습니다.

그 이유는 각 결과에 GetDirectoryEntry()을 호출하기 때문입니다. 이렇게하면 DirectoryEntry 개체가 만들어지며 Active Directory (AD) 개체를 수정해야하는 경우에만 필요합니다. 쿼리가 단일 객체를 반환하지만 AD에 모든 객체를 나열하면 성능이 크게 저하됩니다.

광고를 쿼리하기 만하면 결과 개체의 Properties 컬렉션을 사용하는 것이 좋습니다. 이렇게하면 코드 성능이 여러 번 향상됩니다.

documentation for SearchResult class에 설명되어 다음 SearchResult 클래스의

인스턴스는 DirectoryEntry 클래스의 인스턴스와 매우 유사하다. 결정적인 차이는 SearchResult 대한 데이터가 이미이 쿼리에서 반환됩니다 SearchResultCollection,에서 사용할 수있는 반면 DirectoryEntry 클래스는, 활성 Directory 도메인 서비스 계층 구조에서 새 개체가 에 액세스 할 때마다 해당 정보를 검색 함입니다 은 DirectorySearcher 클래스로 수행됩니다. 여기

Properties 컬렉션을 사용하는 방법에 대한 입니다 : 우리는 번호를 확인하는 Properties["name"].Count를 사용하는 이유 속성이 여러 값을 가질 수

public static List<string> GetComputers() 
{ 
    List<string> computerNames = new List<string>(); 

    using (DirectoryEntry entry = new DirectoryEntry("LDAP://YourActiveDirectoryDomain.no")) { 
     using (DirectorySearcher mySearcher = new DirectorySearcher(entry)) { 
      mySearcher.Filter = ("(objectClass=computer)"); 

      // No size limit, reads all objects 
      mySearcher.SizeLimit = 0; 

      // Read data in pages of 250 objects. Make sure this value is below the limit configured in your AD domain (if there is a limit) 
      mySearcher.PageSize = 250; 

      // Let searcher know which properties are going to be used, and only load those 
      mySearcher.PropertiesToLoad.Add("name"); 

      foreach(SearchResult resEnt in mySearcher.FindAll()) 
      { 
       // Note: Properties can contain multiple values. 
       if (resEnt.Properties["name"].Count > 0) 
       { 
        string computerName = (string)resEnt.Properties["name"][0]; 
        computerNames.Add(computerName); 
       } 
      } 
     } 
    } 

    return computerNames; 
} 

Documentation for SearchResult.Properties

주, 즉 값의

더욱 개선하기 위해 사전에 사용할 속성을 검색 자에게 알리려면 PropertiesToLoad 컬렉션을 사용하십시오. 이를 통해 검색자는 실제로 사용하려고하는 데이터 만 읽을 수 있습니다. DirectoryEntryDirectorySearcher 객체가 제대로 사용하는 모든 리소스를 해제하기 위해 배치 될 해야

참고. 최고 은 using 절로 끝납니다.

관련 문제