views:

119

answers:

1

I have performed an "LDAP://" query to get a list of computers within a specified OU, my issue is not being able to collect just the computer "name" or even "cn".

        DirectoryEntry toShutdown = new DirectoryEntry("LDAP://" + comboBox1.Text.ToString());
        DirectorySearcher machineSearch = new DirectorySearcher(toShutdown);
        //machineSearch.Filter = "(objectCatergory=computer)";
        machineSearch.Filter = "(objectClass=computer)";
        machineSearch.SearchScope = SearchScope.Subtree;
        machineSearch.PropertiesToLoad.Add("name");
        SearchResultCollection allMachinesCollected = machineSearch.FindAll();
        Methods myMethods = new Methods();
        string pcName;
        foreach (SearchResult oneMachine in allMachinesCollected)
        {
            //pcName = oneMachine.Properties.PropertyNames.ToString();
            pcName = oneMachine.Properties["name"].ToString();
            MessageBox.Show(pcName);
        }

Help much appreciated.

+1  A: 

If you can upgrade to .NET 3.5, I would definitely recommend doing so.

With .NET 3.5, you get a new System.DirectoryServices.AccountManagement namespace which makes a lot of these takes much easier.

To find all computers and enumerate them, you'd do something like this:

// define a domain context - use your NetBIOS domain name
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

// set up the principal searcher and give it a "prototype" of what you want to
// search for (Query by Example) - here: a ComputerPrincipal
PrincipalSearcher srch = new PrincipalSearcher();
srch.QueryFilter = new ComputerPrincipal(ctx);;

// do the search
PrincipalSearchResult<Principal> results = srch.FindAll();

// enumerate over results
foreach(ComputerPrincipal cp in results)
{
   string computerName = cp.Name;
}

Check out the Managing Directory Security Principals in the .NET Framework 3.5 on MSDN Magazine for more information on the new S.DS.AM namespace and what it offers.

If you can't move up to .NET 3.5 - you just need to keep in mind that the .Properties["name"] that you get from the search result is a collection of values - so in order to grab the actual pc name, use this:

pcName = oneMachine.Properties["name"][0].ToString();

You need to index the .Properties["name"] collection with a [0] to get the first entry (typically also the only entry - hardly any computer has more than one name).

marc_s
I just added the [0] in before i read your post, worked like a treat :) Thanks again Marc.Im going to have to do some reading up on the collections e.g. Properties, as i don't really understand what they are yet, or how to work out when im dealing with a collection of objects.
Stephen Murby
I am actually using .Net 3.5 i just didn't have the foggiest idea about how to use the .AccountManagement namespace, though it will be something i have a look at as i hope to extend the app i'm writing when i get some more time, for now the bare minimum will do as i need it Monday :)
Stephen Murby
@Stephen Murby: definitely read the MSDN article - excellent stuff!
marc_s