views:

353

answers:

2

Im wondering how to get a list of all computers / machines / pc from active directory?

(Trying to make this page a search engine bait, will reply myself. If someone has a better reply il accept that )

+3  A: 

If you have a very big domain, or have setup limits on your domain how how many items can be returned pr search you might have to use paging.

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;
}
EKS
A: 

An LDAP query like: (objectCategory=computer) should do the trick. -jim

jeemster