Skip to content

List all computers in active directory

November 18, 2014

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;
}

--- -- ---- ------------------------------------------------------------------ ------ - -- -                          -- -             ---------------------

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: