tags:

views:

1450

answers:

3

I want to query Active Directory using VBScript (classic ASP). How can I accomplish that?

+2  A: 

To look at all the members of an OU, try this...

Set objOU = GetObject("LDAP://OU=YourOU,DC=YourDomain,DC=com")
For each objMember in ObjOU  ' get all the members'

    ' do something'

Next

To do a custom search for DNs try this...

set conn = createobject("ADODB.Connection")
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"

strQueryDL = "<LDAP://" & strDefaultNamingContext & ">;(&(objectCategory=person)(objectClass=user));distinguishedName,adspath;subtree"
set objCmd = createobject("ADODB.Command")
objCmd.ActiveConnection = Conn
objCmd.Properties("SearchScope") = 2 ' we want to search everything
objCmd.Properties("Page Size") = 500 ' and we want our records in lots of 500 

objCmd.CommandText = strQueryDL
Set objRs = objCmd.Execute

While Not objRS.eof

 ' do something with objRS.Fields("distinguishedName")'
 objRS.MoveNext
Wend
Ken Hughes
A better way would be to actually use the particular firstname/lastname you are looking for in the query...strQueryDL = "<LDAP://" (distinguishedName,adspath;subtree"...' do something with objRS.Fields("sAMAccountName")'
Ken Hughes
A: 

You want to use Active Directory Service Interfaces (ADSI)

The ADSI Scripting Primer is a good place to start learning and find examples. (btw, these links refer to Windows 2000, but are valid for subsequent versions of Windows as well).

Jay