Skip to content

Instantly share code, notes, and snippets.

@FennyFatal
Created September 11, 2014 00:15
Show Gist options
  • Save FennyFatal/44b7451cfe5917cddbc5 to your computer and use it in GitHub Desktop.
Save FennyFatal/44b7451cfe5917cddbc5 to your computer and use it in GitHub Desktop.
//For easy notification.
public interface NotifyLoaded
{
void LoadComplete(ArrayList computersInDomain);
void oneMore();
}
public static ArrayList getComputersInDomain(string domainName, NotifyLoaded countForm = null)
{
System.Collections.ArrayList computers = new System.Collections.ArrayList();
DirectoryEntry de = new DirectoryEntry("LDAP://" + domainName);
DirectorySearcher mySearcher = new DirectorySearcher(de);
mySearcher.Filter = ("(objectClass=computer)");
mySearcher.PropertyNamesOnly = true;
//For speed we only ask for one property that we will get back anyway.
mySearcher.PropertiesToLoad.Add("adspath");
mySearcher.SizeLimit = int.MaxValue;
mySearcher.PageSize = int.MaxValue;
SearchResultCollection src = mySearcher.FindAll();
//HACKISH, but we are only searching one domain, so we can assume that the start value will be constant throughout the result set.
int start = 0;
if (src[0] != null)
start = src[0].Path.IndexOf("CN=") + 3;
//END HACKISH
foreach (SearchResult c in src)
{
//Being delimited by commas, this is the fastest way to get the computer name.
string name = c.Path.Substring(start).Split(',')[0];
computers.Add(name);
if (countForm != null)
countForm.oneMore();
}
computers.Sort();
if (countForm != null)
countForm.LoadComplete(computers);
return computers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment