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



我读过另一篇关于如何将COMobject从AD中取出到可读日期时间的文章。我现在陷入困境的是如何在active directory中设置帐户的到期日期。我读过关于如何将日期转换回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:Python37libsite-packagespyadaduser.py", line 16, in create
optional_attributes=optional_attributes
File "I:Python37libsite-packagespyadadcontainer.py", line 47, in create_user
pyadutils.pass_up_com_exception(e)
File "I:Python37libsite-packagespyadpyadutils.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)

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

最新更新