代码之家  ›  专栏  ›  技术社区  ›  bogus

Python-pyad用逗号创建用户

  •  1
  • bogus  · 技术社区  · 6 年前

    我正在尝试使用pyad创建一个用户,我的工作是:

        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")
    

    但是如果我试着让这个名字 doe, john

        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")
    

    我知道错误:

    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.
    

    我不知道怎么绕过逗号,我试过转义它,也包括一个chr(44),但没什么用。

    1 回复  |  直到 5 年前
        1
  •  2
  •   Grisha Levit    6 年前

    您必须处理两个字符问题:

    1. RDN(例如。 cn=User Name )LDAP对象的 must 用反斜杠转义以下字符:
      , \ # + < > ; " =

    2. sAMAccountName cannot contain 下列任何字符:
      " [ ] : ; | = + * ? < > / \ ,

    pyad by default 尝试设置 账户名称 name 参数传递给 ADUser.create() 名称 账户名称 作为可选属性。

    pyad.aduser.ADUser.create("Doe\, John", ou, password="Password123", 
        optional_attributes={'sAMAccountName':'jdoe'})
    
    推荐文章