I've read the other posting about how to get the COMobject
out of AD to a readable date time. Where I am stuck at now is how to set a date for an account to expire in active directory. I've read around looking how to convert date back to an adsi_time_com_obj
, but have not found a way to do it in python.
Rest of the code works fine for creating an account as long as I remove the accountexpires
line. The function of this program is to read through a spreadsheet and create accounts that expires in 6 weeks.
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,
})
error message:
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.
I found the solution:
added the lines
dt = datetime.datetime(2019,6,2,2,3,11)
pyad.aduser.ADUser.set_expiration(new_user, dt)
I could not set it as an optional attribute.
User contributions licensed under CC BY-SA 3.0