我已经阅读了另一篇关于如何将COMobjectAD 转换为可读日期时间的帖子。我现在遇到的问题是如何在活动目录中设置帐户过期的日期。我已经阅读了有关如何将日期转换回的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.