Artem Makarov
Artem Makarov

Reputation: 874

Get AD Computer's Description

I need to get domain computer's description. From Registry I can only get local description, but I need Active Directory description. Any ideas?

Looking forward! Thank you!

Upvotes: 3

Views: 7277

Answers (1)

Hans
Hans

Reputation: 13070

Use the DirectoryEntry class (System.DirectoryServices namespace) to connect to Active Directory. Provide a username and password and the LDAP root path to search for computer objects in Active Directory. Then use the DirectorySearcher class to query for the computer object.

The code below shows how to search for a computer with the name computer01. I've also added the description property to the properties to load (not all properties get loaded by default). In the code below you have to replace with the name of your domain controller. By the same token replace the tags with the name of your domain. For example if the name of your Active Directory server is server01 and your domain name is fabrikam.com then the LDAP path is LDAP://server01/dc=fabrikam,dc=com.

using (DirectoryEntry entry = new DirectoryEntry("LDAP://<your-ad-server-name>/dc=<domain-name-part>,dc=<domain-name-part>",
     "Administrator", "Your Secure Password", AuthenticationTypes.Secure))
{
  using (DirectorySearcher adSearcher = new DirectorySearcher(entry))
  {
    string computerName = "computer01";
    adSearcher.Filter = "(&(objectClass=computer)(cn=" + computerName + "))";
    adSearcher.SearchScope = SearchScope.Subtree;
    adSearcher.PropertiesToLoad.Add("description");
    SearchResult searchResult = adSearcher.FindOne();

    Console.Out.WriteLine(searchResult.GetDirectoryEntry().Properties["description"].Value);
  }
}

Please note, that the code above searches the whole Active Directory for the computer object. To search only in the Computers container use the following LDAP path:

LDAP://<your-ad-server-name>/cn=Computers,dc=<domain-name-part>,dc=<domain-name-part>

Upvotes: 2

Related Questions