none
See if computer exists in AD RRS feed

  • Question

  • Hi, 

    I'm fairly new to C#, I came from PowerShell which makes this task really easy.

    I want to search my domain controller in a specific OU to test if a computer account exists.

    In PowerShell I would have just used Get-ADComputer, in C# I'm trying to use the following code which I have found in examples online:

    using (DirectoryEntry entry = new DirectoryEntry("LDAP://domain.lan/CN=company computers/DC=domain/DC=lan"))
    {
    using (DirectorySearcher adsearcher = new DirectorySearcher(entry)) { adsearcher.Filter = "(&(objectClass=computer) (cn=" + computerHostName + "))"; adsearcher.SearchScope = SearchScope.Subtree; adsearcher.PropertiesToLoad.Add("description"); SearchResult searchresult = adsearcher.FindOne(); SetOutputTextbox(value: searchresult.ToString()); } }

    But I am getting the below error:

    An operations error occured, error code:

    Name Value Type
    ErrorCode -2147016672 int
    Message "An operations error occurred.\r\n" string
    Source "System.DirectoryServices" string


    Any help is very appreciated :) 


    Friday, May 3, 2019 8:40 AM

Answers

  • Btw, try the following:

    using System.DirectoryServices;
    using System.DirectoryServices.ActiveDirectory;
    
            public static void FindComputer(string computerHostName)
            {
                DirectoryContext dirCtx = new DirectoryContext(DirectoryContextType.Domain, "domain.lan");
                using (Domain usersDomain = Domain.GetDomain(dirCtx))
                using (DirectorySearcher adsearcher = new DirectorySearcher(usersDomain.GetDirectoryEntry()))
                {
                    adsearcher.Filter = "(&(objectClass=computer) (cn=" + computerHostName + "))";
                    adsearcher.SearchScope = SearchScope.Subtree;
                    adsearcher.PropertiesToLoad.Add("description");
                    SearchResultCollection searchResults = adsearcher.FindAll();
    
                    foreach (SearchResult searchResult in searchResults)
                    {
                        Console.WriteLine(searchResult.Properties["adspath"][0]);
                    }
                }
            }

    The reason why your code don't work is you need to login first. In my code it uses the default credential.

    If you passes "*" as computerHostName, it should list all computer entries in the domain (it searches from "root" instead of individual OU).

    Friday, May 3, 2019 10:30 AM
    Answerer

All replies

  • By default, non-domain-controllers machine accounts are added to "CN=Computers" instead of "CN=Company Computers"

    And I assume you know how to change the domain.lan to the actual values (not always equals to concatenation of the /DC chain values)

    Friday, May 3, 2019 9:53 AM
    Answerer
  • Yep, I just removed the actual names for security :)

    I want to search the subtree of a certain OU (OU=Domain Computers,DC=domain,DC=lan) and see if a computer exists. 

    All the examples I've seen online when searching for an object is to supply the full path which I won't know when searching.

    Friday, May 3, 2019 10:02 AM
  • Btw, try the following:

    using System.DirectoryServices;
    using System.DirectoryServices.ActiveDirectory;
    
            public static void FindComputer(string computerHostName)
            {
                DirectoryContext dirCtx = new DirectoryContext(DirectoryContextType.Domain, "domain.lan");
                using (Domain usersDomain = Domain.GetDomain(dirCtx))
                using (DirectorySearcher adsearcher = new DirectorySearcher(usersDomain.GetDirectoryEntry()))
                {
                    adsearcher.Filter = "(&(objectClass=computer) (cn=" + computerHostName + "))";
                    adsearcher.SearchScope = SearchScope.Subtree;
                    adsearcher.PropertiesToLoad.Add("description");
                    SearchResultCollection searchResults = adsearcher.FindAll();
    
                    foreach (SearchResult searchResult in searchResults)
                    {
                        Console.WriteLine(searchResult.Properties["adspath"][0]);
                    }
                }
            }

    The reason why your code don't work is you need to login first. In my code it uses the default credential.

    If you passes "*" as computerHostName, it should list all computer entries in the domain (it searches from "root" instead of individual OU).

    Friday, May 3, 2019 10:30 AM
    Answerer