Python using pyad to display attributes, not getting logonHours

2.3k Views Asked by At

I've been trying to get logonHours to display, while using the pyad in Python 3.7. When I go to display logonHours, as an output it gives me memory at 0x0000000003049708 etc.

Not sure how to display that data. All other attributes displays properly.

from pyad import *
q = pyad.adsearch.ADQuery()
q.execute_query(
    attributes= ["distinguishedName", "givenName", "userWorkstations","homeDirectory", "homeDrive", "logonHours"],
    where_clause = "objectClass = '*'",
    base_dn = "OU=Graphic Design Students, DC=someplace, DC=com"
)
adoutput = []
for row in q.get_results():
    adoutput.append(row["distinguishedName"])
    adoutput.append(row["givenName"])
    adoutput.append(row["userWorkstations"])
    adoutput.append(row["homeDirectory"])
    adoutput.append(row["homeDrive"])
    adoutput.append(row["logonHours"])
adoutput = [x for x in adoutput if x != None]

print(adoutput)

my output looks like:

<memory at 0x0000000003049708>
<memory at 0x00000000030497C8>
<memory at 0x0000000003049888>
<memory at 0x0000000003049948>
<memory at 0x0000000003049A08>
<memory at 0x0000000003049AC8>
3

There are 3 best solutions below

0
On BEST ANSWER

Use

row["logonHours"].tobytes() 

to get the byte value -- you'll see the same fairly cryptic thing that ADSIEdit shows for the attribute value.

The trick then is turning it into something not cryptic. There's a good explanation of how the value is encoded at https://social.technet.microsoft.com/Forums/exchange/en-US/545552d4-8daf-4dd8-8291-6f088f35c2a4/how-is-the-logon-hours-attribute-set-in-active-directory-windows-server-2008-r2-?forum=winserverDS

0
On

LogonHours is a COM object.

Here is how I handle the logonHours attribute. In my loop, I check the data type:

import pyad.pyadutils
import win32com.client
if isinstance(v, win32com.client.CDispatch):
    value = pyad.pyadutils.convert_datetime(v)

This gives me a datetime object that I can work with. Hope this helps someone.

0
On

Using the article linked in @LisaJ's answer, here are some applications you can use this attribute for. Hopefully this helps someone out.

#Convert the array to a list of which hours each day the account is logged in
weekList = []
for shift_1, shift_2, shift_3 in zip(*[iter(row["logonHours"].tobytes())]*3):
    weekList.append(format(shift_1, '08b') + format(shift_2, '08b') + format(shift_3, '08b'))

#Total the hours for each day
totalHours = {}
for i, (shift_1, shift_2, shift_3) in enumerate(zip(*[iter(row["logonHours"].tobytes())]*3)):
    totalHours[i] = len((format(shift_1, '08b') + format(shift_2, '08b') + format(shift_3, '08b')).replace("0", ""))