Python - pyad create user with a comma in their name

2

I'm trying to create a user using pyad, what I have working is:

    ou = pyad.adcontainer.ADContainer.from_dn('OU=Employees,DC=lan,DC=company,DC=com')
    name =  ("doe john")
    newUser = pyad.aduser.ADUser.create(name, ou, password="password")

However if I try and make the name to be doe, john (has a comma in it now):

    ou = pyad.adcontainer.ADContainer.from_dn('OU=Employees,DC=lan,DC=company,DC=com')
    name =  ("doe, john")
    newUser = pyad.aduser.ADUser.create(name, ou, password="password")

I get the error:

Traceback (most recent call last):
File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site- packages\pyad\adcontainer.py", line 38, in create_user
obj.SetInfo()
File "<COMObject <unknown>>", line 2, in SetInfo
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Active 
Directory', 'An invalid dn syntax has been specified.\r\n', None, 0, -2147016654), None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "NamelyToAD.py", line 49, in <module>
newUser = pyad.aduser.ADUser.create(name, ou, password="Password1")
File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyad\aduser.py", line 16, in create
optional_attributes=optional_attributes
File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyad\adcontainer.py", line 47, in create_user
pyadutils.pass_up_com_exception(e)
File "C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyad\pyadutils.py", line 58, in pass_up_com_exception
raise WIN32_ERRORS.get(info['error_num'], win32Exception)(error_info=info, additional_info=additional_info)
pyad.pyadexceptions.win32Exception: 0x80072032: An invalid dn syntax has been specified.

I don't know how to get around including the comma, I've tried escaping it and including a chr(44) as well, but nothing works.

python
pyad
asked on Stack Overflow Aug 16, 2018 by bogus • edited Apr 19, 2019 by bogus

1 Answer

3

There are two character issues you have to handle:

  1. The RDN (e.g. cn=User Name) of an LDAP object must have the following characters escaped with a backslash:
    , \ # + < > ; " =

  2. The sAMAccountName cannot contain any of the following characters:
    " [ ] : ; | = + * ? < > / \ ,

pyad will by default attempt to set the sAMAccountName attribute to the name argument passed to ADUser.create(), so, if a forbidden character is in name, it is necessary to supply your own sAMAccountName as an optional attribute.

In your example, you would have to do something like:

pyad.aduser.ADUser.create("Doe\, John", ou, password="Password123", 
    optional_attributes={'sAMAccountName':'jdoe'})
answered on Stack Overflow Aug 16, 2018 by Grisha Levit • edited Aug 18, 2018 by Grisha Levit

User contributions licensed under CC BY-SA 3.0