I think two things can be wrong but it's been a long time since I did anything like this so I maybe wrong...
First of all, when do you set the userAccountControl
flag? I seem to remember you should do this after the CommitChanges
for the new entry. So like this:
DirectoryEntry newComputer =
dirEntry.Children.Add("CN=" + ComputerName, "computer");
newComputer.CommitChanges();
newComputer.Properties["userAccountControl"].Value = 0x200;
newComputer.CommitChanges();
Second, can you try setting the UF_WORKSTATION_TRUST_ACCOUNT
flag (0x1000
) instead of UF_NORMAL_ACCOUNT
(0x200
).
Can you also check whether the sAMAccountType
of the entry is SAM_MACHINE_ACCOUNT
(0x30000001
). I think this should be automatic but doesn't hurt to check.