使用LDAP路径获取Active Directory对象的信息



如何通过win32com包使用LDAP路径获取Active Directory对象的所有属性和值
示例:

import win32com.client

ldap_path = "LDAP://CN=aaa,DC=bbb,DC=ccc,DC=eee"
ldap_object = win32com.client.GetObject(ldap_path)

我自己的答案:


import win32com.client

def get_attributes_and_values(ldap_path):
ldap_object = win32com.client.GetObject(ldap_path)
ldap_object.GetInfo()
attributes = [
ldap_object.Item(x).Name
for x in range(0, ldap_object.PropertyCount)
]
for attribute in attributes:
value = getattr(ldap_object, attribute)
yield {attribute: value}

ldap_path = "LDAP://CN=aaa,DC=bbb,DC=ccc,DC=eee"
attr_val = get_attributes_and_values(ldap_path)
for x in attr_val:
print(x)

ItemNamePropertyCount是";COM";已被CCD_ 4包装的对象。