rad_
rad_

Reputation: 301

List not outputting any data (System.DirectoryServices)

I currently have a method that queries for Canonical Name and the CN of a specified machine. Although, when validating the data within the method, nothing is outputted. I'm not sure if I've formatted something incorrectly or didn't filter properly. I've tried a number of things (changing the format of search.Filter), etc., to no avail. Below is the latest code I have. I'm hoping someone will see something that I am missing.

    public class ComputersCheck
    {
        public string OU { get; set; }
        public string ComputerName { get; set; }

        public override string ToString()
        {
            return OU + " " + " " + ComputerName;
        }
    }
    public List<ComputersCheck> GetOU(string PCName)
    {
            List<ComputersCheck> Computers = new List<ComputersCheck>().ToList();
            string DomainPath = "LDAP://DC=DOMAIN,DC=COM"; 
            DirectoryEntry searchRoot = new DirectoryEntry(DomainPath); 
            DirectorySearcher search = new DirectorySearcher(searchRoot);
            search.Filter = "(&(objectClass=Computer)(computerName=MACHINE_NAME))"; 
            search.PropertiesToLoad.Add("CN");
            search.PropertiesToLoad.Add("Canonical Name"); 
            SearchResult result;
            SearchResultCollection resultCol = search.FindAll();

            if (resultCol != null)
            {
                for (int counter = 0; counter < resultCol.Count; counter++)
                {
                    string ComputerNameOUString = string.Empty;
                    result = resultCol[counter];
                    if(result.Properties.Contains("CN") &&
                       result.Properties.Contains("Canonical Name"))
                    {
                        ComputersCheck objSurveyComputers = new ComputersCheck();
                        objSurveyComputers.ComputerName = (String)result.Properties["CN"][0];
                        objSurveyComputers.OU = (String)result.Properties["Canonical Name"][0];
                        Computers.Add(objSurveyComputers);
                    }
                }
            }

        foreach (ComputersCheck computer in Computers)
        {
            Console.WriteLine(computer);

        }
        search.Dispose();
        searchRoot.Dispose();
        return Computers;
    }

Upvotes: 0

Views: 72

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 40998

You do have a few things that need to be corrected:

  1. There is no computerName attribute. The attribute you're after is called sAMAccountName, which is the same attribute used for the username for user accounts. However, for computers, it is the computer name followed by the $ character. So your search should look like this:
(&(objectClass=computer)(sAMAccountName=MACHINE_NAME$))
  1. Attribute names never have spaces. You want the canonicalName attribute. I see you are assigning it to a property called OU. Just be aware that the canonicalName includes the name of the object itself at the end, not just the OU.
search.PropertiesToLoad.Add("canonicalName");
...
objSurveyComputers.OU = (String)result.Properties["canonicalName"][0];
  1. You are calling .Dispose() on a couple objects, which is fine (although not absolutely needed). But the most important one to dispose is resultCol, because the documentation for SearchResultCollection says:

Due to implementation restrictions, the SearchResultCollection class cannot release all of its unmanaged resources when it is garbage collected. To prevent a memory leak, you must call the Dispose method when the SearchResultCollection object is no longer needed.

You can also put it in a using statement instead of calling .Dispose() manually, which would have the same effect.

Upvotes: 1

Related Questions