Win32Com LDAP Enumerate Users

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ken Harvey
    New Member
    • Dec 2010
    • 1

    Win32Com LDAP Enumerate Users

    I am attempting to scan a Windows Active Directory using wincom32.client and LDAP to pull a list of users and their attributes.

    My problem is that I am unable to get the search working:
    Code:
    import win32com.client, win32net
    strFQADDomainName = win32com.client.GetObject('LDAP://rootDSE').Get("defaultNamingContext")
    arrDomainObjects = win32com.client.GetObject("LDAP://" + strFQADDomainName + ">;(&(&(&(objectCategory=user)(name=*))))")
    When I run that I receive the following error:
    arrDomainObject s = win32com.client .GetObject("LDA P://" + strFQADDomainNa me +
    ">;(&(&(&(objec tCategory=user) (name=*))))")
    File "C:\Python26\li b\site-packages\win32c om\client\__ini t__.py", line 72, in
    GetObject
    return Moniker(Pathnam e, clsctx)
    File "C:\Python26\li b\site-packages\win32c om\client\__ini t__.py", line 87, in
    Moniker
    moniker, i, bindCtx = pythoncom.MkPar seDisplayName(P athname)
    pywintypes.com_ error: (-2147463168, 'OLE error 0x80005000', None, None)
    If I just use:
    Code:
    arrDomainObjects = win32com.client.GetObject("LDAP://" + strDomainName)
    Then it lists out the root level OU's. But it will not go any farther than the root level OU's.
    If I hard code the OU path then it will pull the users
    Code:
    arrDomainObjects = win32com.client.GetObject("LDAP://cn=batman,ou=users" + strFQADDomainName)
    My goal is to pull a list of users like this would
    Code:
    strFQADDomainName = win32com.client.GetObject('LDAP://rootDSE').Get("defaultNamingContext")
    strDomainName = '.'.join(strFQADDomainName.split('DC=')).replace(',', '')[1:]
    arrDomainObjects = win32com.client.GetObject("WinNT://" + strDomainName)
    arrDomainObjects.Filter = ["User"]
    If the win32com.client with LDAP does not support a search (with after spending several hours googling, and searching I suspect it doesn't) then is there a way for me to pull fully qualified DN from the WinNT version so that I can run a loop based on the info that I pull from WinNT
    For example:
    Code:
    strFQADDomainName = win32com.client.GetObject('LDAP://rootDSE').Get("defaultNamingContext")
    strDomainName = '.'.join(strFQADDomainName.split('DC=')).replace(',', '')[1:]
    arrDomainObjects = win32com.client.GetObject("WinNT://" + strDomainName)
    arrDomainObjects.Filter = ["User"]
    for lpDomainObjects in arrDomainObjects:
      print lpDomainObjects.dn
    I would prefer to strictly use LDAP for this, but I have a feeling that I will need to use WinNT for part of it too.

    Any suggestions?
Working...