I want to create an user in a Windows Active-Directory to a special OU. I got the right code already that works fine. But I want to create this user to a sub OU e.g.: Company -> User -> Sales. My code is the following:

def addUser():
    pyad.set_defaults(ldap_server="domain.local", username="Administrator", password="mypassword")
    ou = pyad.adcontainer.ADContainer.from_dn("ou=Company, ou=User, ou=Sales, dc=domain, dc=local")
    new_user = pyad.aduser.ADUser.create("Name", ou, password="UserPassword")
    print("sucess")

Is it possible to create the user directly in my OU or do I have to move him afterwards?


Solution 1:

This line is where you are setting the OU where you want to create the user:

ou = pyad.adcontainer.ADContainer.from_dn("ou=Company, ou=User, ou=Sales, dc=domain, dc=local")

But I think you formatted it wrong. If the Sales OU is inside the Users OU and the Users OU is inside the Company OU, then it should look like this:

ou = pyad.adcontainer.ADContainer.from_dn("ou=Sales,ou=User,ou=Company,dc=domain,dc=local")

Notice that there should also be no spaces after each comma.