Search Filter Syntax in Active Directory

Tim Golden tim.golden at viacom-outdoor.co.uk
Thu Sep 30 04:27:53 EDT 2004


[Dirk Hagemann]
| I want to get the properties of all the computer-accounts of an 
| ActiveDirectory structure (Microsoft). I know that could be done by 
| using "Search Filter Syntax" with LDAP-Dialect or SQL-Dialect.
| I found a lot of information about these dialects, but no 
| example how to use this in an python-script.
| Does anybody have a simple example for me, how to get some 
| information out of AD?

I twitch nervously every time I have to pull something
out of AD, but hopefully a couple of working examples
might get you going. In general, anything you see elsewhere
in VBS etc. can be done with a couple of GetObject-type
calls in win32com.client.

First example: find the display name of a user, given a user name.
This one uses the LDAP:// moniker syntax.

<code>

import win32com.client

username = "goldent"
ldap_string = "LDAP://cn=%s,cn=users,dc=gb,dc=vo,dc=local" % username
ldap_me = win32com.client.GetObject (ldap_string)
print username, "=>", ldap_me.Get ("displayName")

</code>

Second example: find all the domains known by this workstation/server.
This example uses the WinNT:// object, which I personally find
a lot easier / more intuitive to use, so long as it meets your
need (it doesn't do everything, and I'm not sure you can update
through it).

<code>

import win32com.client

for domain in win32com.client.GetObject ("WinNT:"):
  print domain.Name

</code>

Third example: list all the computers in a particular domain.
Again WinNT:// syntax.

<code>

import win32com.client
domain_name = "VOUK"
domain = win32com.client.GetObject ("WinNT://" + domain_name)
domain.Filter = ["Computer"]
for computer in domain:
  print computer.Name

</code>

HTH
TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list