Python 使用 pyad 设置帐户的可选属性过期

Python setting optional attribute of account expires using pyad

我已经阅读了另一篇关于如何将 COMobject 从 AD 移出到可读日期时间的帖子。我现在遇到的问题是如何设置帐户在活动目录中过期的日期。我已经阅读了有关如何将日期转换回 adsi_time_com_obj 的方法,但在 python.

中还没有找到方法

只要删除 accountexpires 行,其余代码就可以正常创建帐户。该程序的功能是读取电子表格并创建 6 周后到期的帐户。

from pyad import *
import openpyxl
import logging
import datetime
from dateutil.relativedelta import relativedelta


wb=openpyxl.load_workbook(filename='typing.xlsx')
ws=wb['typing']
max_row = ws.max_row
counter = 0
for i in range(2, max_row +1):
    user = ws['A' + str(i)].value
    user = user + ".type"
    sn = ws['B' + str(i)].value
    givenName = ws['C' + str(i)].value
    disname = sn +" " +givenName
    description = (datetime.date.today() + relativedelta(weeks=6))
    description = description.strftime('%m-%Y')
    description = "Set account to expire on " + description
    ou = pyad.adcontainer.ADContainer.from_dn("ou=Students, DC=domain, DC=com")
    new_user = pyad.aduser.ADUser.create(user, ou, password="Password12345",
                                        optional_attributes={
                                                                            'accountexpires': "2018-12-01",
                                                                            'sn':sn,
                                                                            'givenName':givenName,
                                                                            'displayName':disname,
                                                                             'description':description,
                                                                             })

错误信息:

Traceback (most recent call last):
  File "C:/PycharmProjects/ActiveDirectory/old1.py", line 28, in <module>
    'description':description,
  File "I:\Python37\lib\site-packages\pyad\aduser.py", line 16, in create
    optional_attributes=optional_attributes
  File "I:\Python37\lib\site-packages\pyad\adcontainer.py", line 47, in create_user
    pyadutils.pass_up_com_exception(e)
  File "I:\Python37\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: 0x8007200b: The attribute syntax specified to the directory service is invalid.

我找到了解决方案:

添加了行

dt = datetime.datetime(2019,6,2,2,3,11)
pyad.aduser.ADUser.set_expiration(new_user, dt)

我无法将其设置为可选属性。