Page 1 of 1 1
Topic Options
#139207 - 05/05/05 08:56 PM RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
Chris S. Moderator Offline
Moderator
*****

Registered: 02/03/18
Posts: 2364
I think I'm just about done with my AD ADODB query function. Posting this for additional comments and suggestions.

This function is extremely useful for returning information from anywhere in Active Directory. For example, if you know the name, but not the ADsPath of a user you could use this to return the ADsPath and then use that to connect to that object to modify other properties.

It is also very fast, returning most query results in less than a second.

Code:

;
;Function:
; fnADQuery()
;
;Author:
; Christopher Shilt (christopher.shilt@relizon.com)
;
;Version:
; 1.0 (May 5, 2005)
;
;Version History:
;
;Action:
; Uses ADODB to retrieve information from Active Directory.
;
;Syntax:
; fnADQuery(WHAT, FROM, WHERE, Optional ORDER BY, Optional SCOPE)
;
;Parameters:
; WHAT : Required. Attribute (or array of attributes) to retrieve For
; each object. If you specify *, the query retrieves only the
; ADsPath of each object.
;
; FROM : Required. Specifies the ADsPath of the base of the search.
; For example, the ADsPath of the Users container in an Active
; Directory domain might be 'LDAP://CN=Users,DC=Fabrikam,DC=COM'.
;
; WHERE : Required. Specifies the query filter. You can also add wildcards
; and conditions to an LDAP search filter. The following examples
; show substrings that can be used to search the directory.
;
; Get all users:
; "objectClass = 'Users'"
; Get entries with a common name equal to "bob":
; "cn = 'bob'"
; Get entries with a common name beginning with "bob":
; "cn = 'bob*'"
; Get entries containing "bob" somewhere in the common name:
; "cn = '*bob*'"
; Get entries with "bob" as the common name and "dull" as the surname:
; "cn = 'bob' AND sn = 'dull'"
;
; ORDER BY : Optional. An optional statement that generates a server-side
; sort. Active Directory supports the sort control, but it can
; impact server performance if the results set is large. Active
; Directory supports only a single sort key. You can use the
; optional ASC and DESC keywords to specify ascending or descending
; sort order; the default is ascending.
;
; Order by surname with a ascending sort order:
; "Order By sn"
; Order by surname with a descending sort order:
; "Order By sn DESC"
;
; SCOPE : Optional. Specifies the scope of a directory search.
;
; ADS_SCOPE_BASE = 0
; Limits the search to the base object. The result contains,
; at most, one object.
; ADS_SCOPE_ONELEVEL = 1
; Searches one level of the immediate children, excluding
; the base object.
; ADS_SCOPE_SUBTREE = 2 (DEFAULT)
; Searches the whole subtree, including all the children and
; the base object itself.
;
;Remarks:
;
;
;Returns:
;
; An array of attributes entered in the order specified in the WHAT parameter. If
; only one attribute is specified each element in the array will contain the result
; of the query. If an array of attributes is specified in the WHAT parameter Each
; element in the array will contain an array of the results of the query.
;
; Sets the value of @ERROR based on success/failure.
;
;Dependencies:
;
;
;Example:
;
; ; == Return the Name and AdsPath of all users ===================
; $aWhat = "Name", "AdsPath"
; $sFrom = "LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext")
; $sWhere = "objectClass = 'User' AND Name = '*'"
; $sOrderBy = "Order By Name"
;
; $aResults = fnADQuery($aWhat,$sFrom,$sWhere,$sOrderBy)
; @ERROR " | " @SERROR ?
;
; For Each $Result in $aResults
; If VarType($Result)>8192
; For Each $R in $Result
; $R ?
; Next
; Else
; $Result ?
; EndIf
; Next
;
; ; == Return the Name and AdsPath of all users beginning in 'S' ==
; $aWhat = "Name", "AdsPath"
; $sFrom = "LDAP://"+GetObject("LDAP://rootDSE").Get("defaultNamingContext")
; $sWhere = "objectClass = 'User' AND Name = 'S*'"
; $sOrderBy = "Order By Name"
;
; $aResults = fnADQuery($aWhat,$sFrom,$sWhere,$sOrderBy)
; @ERROR " | " @SERROR ?
;
; For Each $Result in $aResults
; If VarType($Result)>8192
; For Each $R in $Result
; $R ?
; Next
; Else
; $Result ?
; EndIf
; Next
;
;
Function fnADQuery($What,$From,$Where,Optional $OrderBy, Optional $Scope)
Dim $nul,$sQ,$oCon,$oCMD,$oRS,$sF,$sGV,$R,$vP,$aR[0]

If $Scope>2 Exit 87 EndIf

$sQ="Select "+Iif(VarType($What)>8192,Join($What,','),$What)+
" from "+"'"+$From+"' where "+$Where+" "+$OrderBy

If VarType($What)>8192
For Each $sF in $What $sGV=$sGV+'$'+'oRS.Fields("'+$sF+'").Value,' Next
$sGV=Substr($sGV,1,Len($sGV)-1)
Else
$sGV='$'+'oRS.Fields("'+$What+'").Value'
EndIf

$oCon=CreateObject("ADODB.Connection")
$oCon.Provider="ADsDSOObject"
$oCon.Open("Active Directory Provider")
If @ERROR Exit @ERROR EndIf

$oCMD = CreateObject("ADODB.Command")
$oCMD.ActiveConnection=$oCon
$oCMD.CommandText=$sQ
$oCMD.Properties("Page Size").Value=1000
$oCMD.Properties("Timeout").Value=30
$oCMD.Properties("Searchscope").Value=Iif(VarType($Scope)=3,$Scope,2)
$oCMD.Properties("Cache Results").Value=NOT 1

$oRS=$oCMD.Execute
If @ERROR OR ($oRS.BOF AND $oRS.EOF) Exit @ERROR EndIf

Do
$nul=Execute('$'+'vP='+$sGV)
$aR[$R]=$vP
$oRS.MoveNext
$R=$R+1
ReDim Preserve $aR[$R]
Until $oRS.EOF
ReDim Preserve $aR[$R-1]
$fnADQuery=$aR
EndFunction



Edited by Chris S. (05/05/09 11:09 PM)

Top
#139208 - 05/05/09 03:45 PM Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
Chris S. Moderator Offline
Moderator
*****

Registered: 02/03/18
Posts: 2364
Has anyone had the time to try this out yet? Any questions?
Top
#139209 - 05/05/09 11:10 PM Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
Chris S. Moderator Offline
Moderator
*****

Registered: 02/03/18
Posts: 2364
I edited the original to correct an issue with defining the scope of the search.
Top
#139210 - 05/05/09 11:17 PM Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 02/05/13
Posts: 232
Loc: STRASBOURG, France
I have tested the first function but I never have result.
BOF and EOF seem always true !!! (no test with the modified version)

As I have already written something similar to fnADQuery, i'll try to prepare an example.
I am not using SQL, i am using LDAP syntax.
LDAP syntax is not easier but it's very powerful and the server does all the work !!!


Edited by Christophe Melin (05/05/09 11:20 PM)

Top
#139211 - 05/05/10 01:00 AM Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
Sealeopard Moderator Offline
Moderator
*****

Registered: 01/04/25
Posts: 11153
Loc: Boston, MA, USA
BOF and EOF will always mark the first and last record if BOF=EOF then the resulting set is empty.
_________________________
There are two types of vessels, submarines and targets.

Top
#139212 - 05/05/10 03:29 AM Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
Chris S. Moderator Offline
Moderator
*****

Registered: 02/03/18
Posts: 2364
Christophe, thanks for trying it. Did you use the latest? I edited the original due to an error in the scope. Basically it was defaulting to Scope = 0 instead of Scope =2.
Top
#139213 - 05/05/11 05:17 PM Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 02/05/13
Posts: 232
Loc: STRASBOURG, France
I have tested the modified version and it works fine.

I suggest you rename the function in fnSQLADQuery (because it uses SQL syntax).
I am going to start a new thread for fnLDAPADQuery. The goal is the same than your function but I use LDAP syntax for the search filter.
_________________________
Christophe

Top
#139214 - 05/05/11 05:25 PM Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
Chris S. Moderator Offline
Moderator
*****

Registered: 02/03/18
Posts: 2364
Don't work too hard on it. :-) I already have a version.
Top
#139215 - 05/05/11 05:42 PM Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 02/05/13
Posts: 232
Loc: STRASBOURG, France
my LDAP version for fnLDAPADQuery is ready.

I just have to postprep it and write the post.
_________________________
Christophe

Top
#139216 - 05/05/11 06:02 PM Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
Chris S. Moderator Offline
Moderator
*****

Registered: 02/03/18
Posts: 2364
Mine too. See: fnLDAPQuery()
Top
Page 1 of 1 1


Moderator:  Sealeopard, Glenn Barnas, NTDOC, Bryce, Jochen, Radimus, Kdyer, Howard Bullock, Chris S., Allen, Benny69, Mart 
Hop to:
Shout Box

Who's Online
4 registered (Allen, jvd626, JSP, kholm) and 47 anonymous users online.

Generated in 0.1 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org