[python-win32] enumerating children of an active directory container

Jens B. Jorgensen jens.jorgensen@tallan.com
Mon, 09 Dec 2002 10:26:13 -0600

This is a multi-part message in MIME format.
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Attached is a very short script that demonstrates child enumeration. You 
give it a path and it prints out everything in that node and all child 

matt wilbert wrote:

>I am trying to do some work with Active Directory from python 
>(ActivePython 2.2.1), but I am having a couple of problems.
>Connecting to the domain root using an LDAP:// name works fine,
>I can see various properties of the object thus named, , but I
>cannot figure out a way to enumerate its children--it appears to
>be unimplemented.  Is that correct?  Should I just use ADO?
>Also, I can't figure out how I would apply a filter, assuming
>that I could do an enumeration.
>Matt Wilbert
>Python-win32 mailing list

Jens B. Jorgensen

"With a focused commitment to our clients and our people, we deliver value through customized technology solutions"  

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;

from win32com.client import GetObject
import sys

if len(sys.argv) < 2 :
    print 'usage: %s <ADSI path>' % sys.argv[0]
    print 'example: %s WinNT://localhost' % sys.argv[0]

o = GetObject(sys.argv[1])

def walk(obj, ind) :
    print '%s%s %s' % (' ' * ind, obj.ADsPath, obj.Class)
    en = obj._NewEnum()
    # ok, we'll print out the Properties
    sch = GetObject(obj.Schema)
    for i in sch.MandatoryProperties :
        try :
            print '%s%s = %s' % (' ' * (ind+2), i, obj.Get(i))
        except Exception, e :
    for i in sch.OptionalProperties :
        try :
            print '%s%s = %s' % (' ' * (ind+2), i, obj.Get(i))
        except Exception, e :
    if en :
        while 1 :
            i = en.Next()
            if len(i) > 0 :
                walk(i[0], ind+2)
            else :

walk(o, 0)