#139207 - 2005-05-05 08:56 PM
RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
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. (2005-05-09 11:09 PM)
|
|
Top
|
|
|
|
#139216 - 2005-05-11 06:02 PM
Re: RFC: fnADQuery() - Uses ADODB to retrieve information from Active Directory
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
Mine too. See: fnLDAPQuery()
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 476 anonymous users online.
|
|
|