I am trying to programmatically add a computer to the Active Directory of my company.
I was searching the internet for so long now, but i couldn't find a solution.
My code:
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://OU=ou2example,OU=ou1example,DC=site,DC=company,DC=com");
dirEntry.Username = "username";
dirEntry.Password = "password";
DirectoryEntry newComputer = dirEntry.Children.Add("CN=" + ComputerName, "computer");
newComputer.CommitChanges();
My problem:
The computer is added to the Active Directory. But it is flagged as disabled.
I tried to following to enable the computer:
newComputer.Properties["userAccountControl"].Value = 0x200;
But I get an DirectoryServicesCOMException --> The server can not complete the request.
or
newComputer.Properties["Enabled"].Value = true;
But I get an DirectoryServicesCOMException --> The requested operation does not satisfy at least one constraint that is for this object class condition.
Please note that the exceptions are translated from german to english!
Thanks for helping!