locked
LDAP Query for Computer Account attributes RRS feed

  • Question

  • Hi,

    I am trying to retrieve attributes of a Computer account from the AD. I want to retrieve department and location information from a computer account. The problem is, when I set objectCategory=Computer) I am not getting above mentioned properties in the properties collection. At the same time if I set  objectCategory=Person), I am able to see these attributes.

    Here is the code I am working on.

    DirectoryEntry entries = new DirectoryEntry("LDAP://MYDOM");
       
       DirectorySearcher searcher = new DirectorySearcher(entries, "(objectCategory=Computer)");
    
       searcher.PropertyNamesOnly = true;
       searcher.PropertiesToLoad.Add("department");
       searcher.PropertiesToLoad.Add("location");
       searcher.PropertiesToLoad.Add("cn");
    
       foreach(SearchResult entry in searcher.FindAll())
       {
        Console.Write(entry.GetDirectoryEntry().Properties["CN"].Value.ToString()+" - ");
        if (entry.GetDirectoryEntry().Properties["department"].Value != null)
        {
         Console.Write(entry.GetDirectoryEntry().Properties["department"].Value.ToString());
        }
        Console.WriteLine("");
        
        
       }
    
    




     Arun D Gangadharan

     

    Wednesday, May 26, 2010 6:32 AM

Answers

  • Hi Eryang,

    I'm sorry for the late reply. If there is no "Location" attribute set for a particular computer account then the search result wont contain that property in its property collection. I made a mistake in expecting this property in the property collection even if it is not set in the AD.

    Andre, Yes there is an attribute "Location" for a Computer account.

    Thank you all for valuable inputs.

     

    -Arun


    Arun D Gangadharan
    Friday, June 4, 2010 12:30 AM

All replies

  • Hi Arun,

    take a look with your Directory application to take a look at the different properties of computer accounts.

    If you're running on Win2k3 or Win2k8 you can use Start -> Run -> dsa.msc

    This will show the management console of the Active Directory Users and Computers.

    regards

    Wednesday, May 26, 2010 7:06 AM
  • Hi Andre,

    Thanks for the reply.

    I want to get the location, department and description information which are set in the AD by the Admin team. The problem with my code is, the properties doesn't contain location or department when I query for Computers. Only description is there in the SearchResult's property collection. But if I query for the Users (ObjectCategory=Person), the SearchResult contains both department and location.

    Based on my requirement, I expect atleast an empty string if these values are not set in the AD.

    Any idea what could be the issue?

    Thank you,

    Arun


    Arun D Gangadharan
    Wednesday, May 26, 2010 8:18 AM
  • Hi Arun,

    When I look at the Active Directory, the computer accounts do not have properties like department or location.

    regards

    Wednesday, May 26, 2010 9:23 AM
  •  

    Hi Arun,

    I'm writing to check the issue status, please feel free to let us know if you have any concern.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, June 3, 2010 1:58 AM
  • Hi Eryang,

    I'm sorry for the late reply. If there is no "Location" attribute set for a particular computer account then the search result wont contain that property in its property collection. I made a mistake in expecting this property in the property collection even if it is not set in the AD.

    Andre, Yes there is an attribute "Location" for a Computer account.

    Thank you all for valuable inputs.

     

    -Arun


    Arun D Gangadharan
    Friday, June 4, 2010 12:30 AM
  • How do I set properties like "department" and "location" using C# ?

    I am having the same problem as Arun. I want to set the values of the properties "department" and location for the computer object. But when I use the above code (Arun's original code in post 1) I don't get "department" or "location" property in the properties collection.

    The department and location attributes have been added to the computer object, still I can't retrieve them.

    According to the above posts it looks like the properties have to be "set" in AD? How do I "set" the properties in AD ? Any help appreciated.

     

    Wednesday, June 16, 2010 3:22 PM
  • I was able to set the properties.

    But still cannot retrieve {not set} properties

    Thursday, June 17, 2010 6:12 PM
  • Hi AgentM840

    Same problem here too. I had to handle the situation with some logic.

     

    -Arun


    Arun D Gangadharan
    Saturday, June 26, 2010 1:16 AM
  • Hi Arun,

    You may consider posting the question at LDAP forum for quick and better support.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, June 28, 2010 11:30 AM