0

I'm rebuilding my PowerShell script with Python. Sole purpose of that is to easily insert data to Postgre DB tables with Python. PowerShell script works as it should, but when it comes to Python I encountered a obstacle.

In this loop, I gather all data - most like direct reports from specific AD account. As you may see, I already gather account name, name/surname, e-mail address and password last set date. The problem is to calculate password age. In PS it was pretty easy, but when I ran a code in Python, I get an Exception has occurred: TypeError can't subtract offset-naive and offset-aware datetimes

Here is the part of the code:

reports = getDirectReports(managers_name)
for users in reports:
    for user in users.directReports.values:
            if 'cn=ext.' in user.lower():

                user_details = getUserDetails(user)
                print(user_details[0].cn.value)
                print(user_details[0].givenName.value)
                print(user_details[0].sn.value)
                print(user_details[0].pwdLastSet.value)
                print(user_details[0].mail.value)
                current_date = datetime.now()
                start_date = user_details[0].pwdLastSet.value
                if (start_date == 0):
                    password_age = 'NULL'
                else:
                    password_age = current_date - start_date    ```



  • Can you please print `print(type(current_date))` and `print(type(start_date))`. Your error suggests these do not have the same type, one seems to be offset-aware while the other one seems not to be – DataJanitor Jan 05 '23 at 08:41
  • 2
    Does this question and answer help? https://stackoverflow.com/questions/796008/cant-subtract-offset-naive-and-offset-aware-datetimes – JohnFrum Jan 05 '23 at 08:43
  • @Jan From `print(type(current_date))` I got a results like `Name Surname 2022-08-24 08:01:52.270641+00:00 Name.Surname@mail.com ` . From `print(type(start_date))` I got the same results. I'm not sure if I did that correctly, as you suggest. –  Jan 05 '23 at 08:51
  • @JohnFrum not really, but it is highly possible that I'm totally crap in coding and typed `now = datetime.now(timezone.utc)` in wrong place, because I receive another error: `Exception has occurred: AttributeError 'function' object has no attribute 'utc'` –  Jan 05 '23 at 09:02
  • Python seems to think that `timezone` is a function, if I am reading that correctly. If I do `from datetime import datetime, timezone` then I can do `now = datetime.now(timezone.utc)` without errors. From your original question, it looks like one of your datetime objects, either current_date or start_date has a timezone and one does not. You'll need to find out which one, perhaps. – JohnFrum Jan 05 '23 at 11:32
  • @JohnFrum the good news is that I do not see any error while running that code, after I add your suggestion line. The bad news is that I do not see any difference, to be honest. Well, it prints all require data but not `password_age` , which in this case is a subtraction result of current date and date of last password set. At least that is my goal. Another strange thing that I spot is date of accounts where there is no last password set date. My code should check if there is a 0 and for these accounts print NULL as value. But it does not. –  Jan 05 '23 at 11:42
  • I think that for us to help any further, we would need to have access to some data of the kind that is returned by your function `getUserDetails` - what does that do? – JohnFrum Jan 05 '23 at 12:00
  • @JohnFrum . Basically, function getUserDetails retrive listed data of user directly reports to specific AD user. `def getUserDetails(distinguishedName): conn.search( search_base='OU=COMPANY.COM,DC=COMPANY,DC=COM', search_filter=f'(distinguishedName={distinguishedName})', attributes = [ 'givenName', 'cn', 'sn', 'mail','pwdLastSet']) return conn.entries` –  Jan 05 '23 at 12:26
  • ok, so this suggests that you have all your pwdLastSet date/time values stored in the database (?) in a particular format, and perhaps that is being misinterpreted by Python or is being stored in a format that Python doesn't like? It may be the case that we'd need that data itself in order to be able to resolve your problem, but I hope you've gathered enough information so far and from that other question (and similar ones) to work out a solution. – JohnFrum Jan 05 '23 at 14:22
  • 1
    @JohnFrum in fact, John, **your line of code somehowe helped me** and I would like to thank you for help. Right now, I could run a code wit successful print "list" of the users with demanded paramters. Also, I could calculate password age by subtraction. My only problem, at this moment, is that some of the users have strange pwdLastSet - like 1601-01-01 00:00:00. These users are unactive, yet I cannot convert that value into NULL like my code suggest. –  Jan 05 '23 at 14:33

0 Answers0