See if computer exists in AD

  • Thread starter Thread starter I.T Delinquent
  • Start date Start date
I

I.T Delinquent

Guest
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 :)

Continue reading...
 
Back
Top