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

时间:2018-11-15 14:45:12

标签: python-3.x datetime active-directory comobject pyad

我已经阅读了另一篇有关如何使COMobject脱离广告的日期范围的文章。我现在遇到的问题是如何为活动目录中的帐户设置过期日期。我已经看过有关如何将日期转换回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.

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

添加了行

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

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