[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.
--------------040008070605010407060102
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
nodes.
matt wilbert wrote:
>Hi,
>
>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.
>
>Thanks,
>
>Matt Wilbert
>matt.wilbert@state.ma.us
>
>
>
>_______________________________________________
>Python-win32 mailing list
>Python-win32@python.org
>http://mail.python.org/mailman/listinfo/python-win32
>
>
--
Jens B. Jorgensen
jens.jorgensen@tallan.com
"With a focused commitment to our clients and our people, we deliver value through customized technology solutions"
--------------040008070605010407060102
Content-Type: text/plain;
name="walkads.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="walkads.py"
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]
sys.exit(1)
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 :
pass
for i in sch.OptionalProperties :
try :
print '%s%s = %s' % (' ' * (ind+2), i, obj.Get(i))
except Exception, e :
pass
if en :
while 1 :
i = en.Next()
if len(i) > 0 :
walk(i[0], ind+2)
else :
break
walk(o, 0)
--------------040008070605010407060102--