The following C# code creates a windows domain user account
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DOMAIN"))
{
UserPrincipal uc = new UserPrincipal(pc, "username", "password", true);
uc.Save();
}
How can I specify the OU at the time of account creation? It seems like it should be simple but I am having no luck. I tried using LDAP but gave up after several "Unknown error (0x80005000)" messages. Is there a way to do it using UserPrincipal, GroupPrincipal or some other Principal that I haven't yet found?
You should do it like that:
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DOMAIN", "OU=ou1" , "OU=ou2" , DC=yourdomain, DC=com, username, password))
This is from my code:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "IPAddress", "OU=NEW, OU=CloudUsers, DC=yourdomain, DC=com", username, password);
I'm sorry I cannot test it right now, but I think you should use the PrincipalContext constructor that lets you specify your OU as container: http://msdn.microsoft.com/en-us/library/bb348316(v=vs.110).aspx
To add new OU, fowlling this code:
GroupPrincipal newOU = new GroupPrincipal(pc);
newOU.Name = "yourOUName";
newOU.Save();
User contributions licensed under CC BY-SA 3.0