I have created a toolbox to create computers in various management systems (including Active Directory). This toolbox has worked flawlessly for years. Since this month the creation of the computer object in Active Directory does not work anymore. I am still looking for the cause, but seems to be related to the patch KB5008383 on the Domain Controllers. Microsoft Update Information
Last December 2021 I faced the same issue - also related to a Microsoft patch - which I could fix with a $ at the end of the SAMAccountName. Nothing has been changed on the user that creates the object. When I manually create a client with the user credentials using the following command, it works fine.
New-ADComputer -Name NB89991 -Path "ou=nb,ou=w10,ou=clt,ou=tier2,ou=central,dc=xy-dom,dc=xy,dc=ch" -SAMAccountName NB89991
Do you know what could be wrong with my code?
public Boolean createComputerAccount(Client computer){
Boolean returnValue = true;
try {
if (!existComputer(computer))
{
PrincipalContext oPrincipalContext = GetPrincipalContext(computer.ou);
ComputerPrincipal computerPrincipal = new ComputerPrincipal(oPrincipalContext);
computerPrincipal.SamAccountName = computer.name + "$";
computerPrincipal.Name = computer.name;
if (!(computer.adDescription == "")) {
computerPrincipal.Description = computer.adDescription;
}
MessageBox.Show(computerPrincipal.SamAccountName, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(computerPrincipal.Name, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
computerPrincipal.Enabled = true;
computerPrincipal.Save();
returnValue = true;
}
else {
returnValue = false;
}
}catch (Exception e){
errorMessage = "Creating a computer in Active Directory failed!\r\nPlease contact the ITCM TEAM or check the Logfile:\r\n (c:\\temp\\ClientToolbox.log).";
createErrorMessage(errorMessage, e);
returnValue = false;
}
return returnValue;
}
public PrincipalContext GetPrincipalContext(string sOU) {
PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, domain, sOU, ContextOptions.SimpleBind, ntUser, ntUserPWD);
return oPrincipalContext;
}
Error Message:
5/27/2022 11:58:32 AM System.UnauthorizedAccessException: Access is denied.
at System.DirectoryServices.AccountManagement.ADStoreCtx.Insert(Principal p) at System.DirectoryServices.AccountManagement.Principal.Save()
at ClientToolbox.ActiveDirectory.createComputerAccount(Client computer)