pythonpython-2.7active-directorypyad

How to create a new user in Active directory using Python pyad module


I am trying to create 100 000 users in Active Directory with different attributes. I am trying this with pyad module as shown below:

from pyad import *


user = aduser.ADUser.from_cn("test")
pyad.set_defaults(ldap_server="blr.test.local", username="Administrator", password="test@123")
ou = ADContainer.from_dn("ou=users, dc=test, dc=local")
new_user = ADUser.create("ADUSER123123", ou, password="Secret123")

But I am getting below error:

C:\Users\Administrator\Desktop>python ad_create_user.py

Traceback (most recent call last):
  File "ad_create_user.py", line 6, in <module>
    ou = ADContainer.from_dn("ou=users, dc=test, dc=local")
NameError: name 'ADContainer' is not defined

C:\Users\Administrator\Desktop>

I have installed pyad module. And I am trying this in Windows server 2008 R2.


Solution

  • I have just had this error and I figured out how to resolve it, please find below the piece of code so you can compare.

    from pyad import *
    
    pyad.set_defaults(ldap_server="IT-LHQ-DC1.LIFEALIKE.LAB", username="administrator", password="password")
    
    ou = pyad.adcontainer.ADContainer.from_dn("ou=All_Users, dc=LIFEALIKE, dc=LAB")
    new_user = pyad.aduser.ADUser.create("test.02", ou, password="password")
    

    Basically, it is not calling correctly the function if you don't type it as pyad.adcontainer.ADcontainer.from_dn(...)

    I hope this could help you and it make any sense for you.

    Saul