Seaching Active Directory via ADO

L

LittlePython

I am have trouble finding a simple working example of using ADO to search
Active Directory. I am hoping someone could point me to a generic working
script that connects to AD and pulls up a recordset to help me get started
into the right direction in learning ADO, ADSI on Python.
 
R

Roger Upole

Here's a short example that uses ADO to search for a
user by wildcard.

import win32com.client
c = win32com.client.Dispatch("ADODB.Connection")
c.Open("Provider=ADSDSOObject")

rs,rc=c.Execute("""
SELECT adspath, title, name
From 'LDAP://DC=yourdomain, DC=COM'
where objectClass='user' and name='Roger*'
""")

while not rs.EOF:
for f in rs.Fields:
print f.Name, f.Value
rs.MoveNext()

hth
Roger
 
R

Roger Upole

You could also accomplish the same thing using the
Command object, but this way is usually more concise
for plain Sql.

Roger
 
L

LittlePython

Thanks but I was looking more for ADO com object than ADSI or ldap.
For some strange reason it is very hard to locate any working scripts that
use ADO to connect and search AD. Is there an issue with ADO and python
when connecting to AD?
I have try to build one myself with no luck. I think my problem is with
adodb.command calls.

Thanks for your response.
 
L

LittlePython

To be more clear, something like this example but in python. I have tryed to
convert something very simular to this
but have failed.

------ SCRIPT CONFIGURATION ------
strBase = "<LDAP://<BaseDN>>;"
strFilter = "<Filter>;"
strAttrs = "<AttrList>;"
strScope = "<Scope>"
' ------ END CONFIGURATION ---------

set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
set objComm = CreateObject("ADODB.Command")
objComm.ActiveConnection = objConn
objComm.Properties("Page Size") = 1000
objComm.CommandText = strBase & strFilter & strAttrs & strScope
set objRS = objComm.Execute
objRS.MoveFirst
while Not objRS.EOF
Wscript.Echo objRS.Fields(0).Value
objRS.MoveNext
wend
 
L

LittlePython

I notice there is no adodb.command. This is not required?
Thx for the example!
Roger Upole said:
Here's a short example that uses ADO to search for a
user by wildcard.

import win32com.client
c = win32com.client.Dispatch("ADODB.Connection")
c.Open("Provider=ADSDSOObject")

rs,rc=c.Execute("""
SELECT adspath, title, name
From 'LDAP://DC=yourdomain, DC=COM'
where objectClass='user' and name='Roger*'
""")

while not rs.EOF:
for f in rs.Fields:
print f.Name, f.Value
rs.MoveNext()

hth
Roger


http://www.rallenhome.com/books/adcookbook/src/18.6-rootdse_python_com.py.tx
http://www.rallenhome.com/books/adcookbook/src/18.6-rootdse_python_ldap.py.t



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----
 
L

LittlePython

Do you know what this may mean?

C:\Documents and Settings\Administrator\Desktop\pytest>ADOSeach.py
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\Desktop\pytest\ADOSeach.py",
lin
e 6, in ?
rs,rc=c.Execute("""
File "<COMObject ADODB.Connection>", line 3, in Execute
File "C:\Python24\Lib\site-packages\win32com\client\dynamic.py", line 258,
in
_ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType,
argTypes
) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Provider',
'Tabl
e does not exist.', None, 1240640, -2147217865), None)

C:\Documents and Settings\Administrator\Desktop\pytest>
Roger Upole said:
You could also accomplish the same thing using the
Command object, but this way is usually more concise
for plain Sql.

Roger


http://www.rallenhome.com/books/adcookbook/src/18.6-rootdse_python_com.py.tx
http://www.rallenhome.com/books/adcookbook/src/18.6-rootdse_python_ldap.py.t



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption
=----
 
L

LittlePython

Never mind , I know what's wrong ... need to use the right account. It works
great and is a great example .. thx
 
L

LittlePython

With help I have been able to put together a little example. It illustrates
several different ways..


import win32com.client

c = win32com.client.Dispatch("ADODB.Connection")
c.Open("Provider=ADSDSOObject")

##Check if connected to AD
if bool(c.state): print "Connected to AD"

## This uses sql dialect with no command object
##rs,rc=c.Execute("""
##SELECT adspath, title, name
##From 'LDAP://DC=AD,DC=LOCAL'
##where objectCategory='Person' and objectClass='user' and name='*'
##""")

##This uses ADSI dialect with not command object
##rs,rc=c.Execute("""
##<LDAP://DC=AD,DC=LOCAL>;\
##(&(objectCategory=Person)(objectClass=user)(name=*));\
##name,adspath,title;\
##subtree
##""")

##Command com with properties in sql dialect
##comm = win32com.client.Dispatch("ADODB.Command")
##comm.ActiveConnection = c
##comm.Properties('Page size').value=1000
##comm.CommandText = ("""\
##SELECT adspath, title, name \
##From 'LDAP://DC=AD,DC=LOCAL' \
##where objectCategory='Person' and objectClass='user' and name='*'\
##""")
##rs,rc=comm.Execute()

##Command com with properties in ADSI dialect
##ADS_SCOPE_SUBTREE = 2
##ADS_SCOPE_ONELEVEL = 1
##ADS_SCOPE_BASE = 0
##comm = win32com.client.Dispatch("ADODB.Command")
##comm.ActiveConnection = c
##comm.Properties('Page size').value=1000
##comm.Properties('searchscope').value=ADS_SCOPE_SUBTREE
##comm.CommandText = ("""<LDAP://DC=AD,DC=LOCAL>;\
##(&(objectCategory=Person)(objectClass=user)(name=*));\
##name,adspath,title;""")
##rs,rc=comm.Execute()

##Connect using recordset object in ADSI Dialect
##rs = win32com.client.Dispatch("ADODB.recordset")
##rs.ActiveConnection = c
##rs.source = ("""<LDAP://DC=AD,DC=LOCAL>;\
##(&(objectCategory=Person)(objectClass=user)(name=*));\
##name,adspath,title;\
##subtree""")
##rs.Open()

##Connect using recordset object in sql Dialect
##rs = win32com.client.Dispatch("ADODB.recordset")
##rs.ActiveConnection = c
##rs.source = ("""\
##SELECT adspath, title, name \
##From 'LDAP://DC=AD,DC=LOCAL' \
##where objectCategory='Person' and objectClass='user' and name='*'\
##""")
##rs.Open()

##while not rs.EOF:
## for f in rs.Fields:
## print f.Name, f.Value
## rs.MoveNext()

c.close
c = None
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top