#139142 - 2005-05-04 10:31 PM
Re: Find Computers using AD Site and Domain?
|
Chris S.
MM club member
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
Ok, I have reversioned my fnADODBQuery prototype again. This version will return a value or an array of values based on a search.
ADODBQuery should return results much faster than a regular LDAP query.
Let me know what you think.
Code:
;========================================================================== ; ; NAME: fnADODBQuery ; ; AUTHOR: Christopher Shilt (christopher.shilt@relizon.com) ; DATE : 7/15/2004 ; ; COMMENT: Uses ADODB to retrieve information from Active Directory ; ;========================================================================== $=SetOption("WrapAtEOL","on") $aWhat = "Name" $sFrom = "LDAP://DC=your,DC=domain,DC=com" $sWhere = "objectClass = 'computer' and name = 'ATL*'" $aResults = fnADODBQuery($aWhat,$sFrom,$sWhere) For Each $Result in $aResults $Result ? Next Get $ Function fnADODBQuery($What,$From,$Where,Optional $Scope) Dim $objconnection, $objCommand, $objRecordSet If NOT $Scope $Scope = 2 EndIf If VarType($What)>8192 $sWhat=Join($What,',') Else $sWhat=$What EndIf $sQuery = "Select " + $sWhat + " from " + "'" + $From + "' where " + $Where $objConnection = CreateObject("ADODB.Connection") $objCommand = CreateObject("ADODB.Command") $objConnection.Provider = "ADsDSOObject" $objConnection.Open("Active Directory Provider") $objCommand.ActiveConnection = $objConnection $objCommand.CommandText = $sQuery $objCommand.Properties("Page Size").Value = 1000 $objCommand.Properties("Timeout").Value = 30 $objCommand.Properties("Searchscope").Value = $Scope $objCommand.Properties("Cache Results").Value = NOT 1 $objRecordSet = $objCommand.Execute $objRecordSet.MoveFirst If VarType($What)>8192 For Each $Field in $What $GetValue = $GetValue + '$' + 'objRecordSet.Fields("' + $Field + '").Value,' Next $GetValue=Substr($GetValue,1,Len($GetValue)-1) Else $GetValue = '$' + 'objRecordSet.Fields("' + $What + '").Value' EndIf Dim $aRecords[0] $r = 0 Do $nul=Execute('$'+'aProperties = ' + $GetValue) $aRecords[$r] = $aProperties $objRecordSet.MoveNext $r = $r + 1 ReDim Preserve $aRecords[$r] Until $objRecordSet.EOF ReDim Preserve $aRecords[$r-1] $fnADODBQuery = $aRecords EndFunction
|
Top
|
|
|
|
#139143 - 2005-05-04 10:43 PM
Re: Find Computers using AD Site and Domain?
|
Chris S.
MM club member
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
Here is a version that sorts the results...
Code:
;========================================================================== ; ; NAME: fnADODBQuery ; ; AUTHOR: Christopher Shilt (christopher.shilt@relizon.com) ; DATE : 7/15/2004 ; ; COMMENT: Uses ADODB to retrieve information from Active Directory ; ;========================================================================== $=SetOption("WrapAtEOL","on") $aWhat = "Name" $sFrom = "LDAP://DC=your,DC=domain,DC=com" $sWhere = "objectClass = 'computer' and name = ATL*'" $sOrderBy = "order by name" $aResults = fnADODBQuery($aWhat,$sFrom,$sWhere,$sOrderBy) For Each $Result in $aResults $Result ? Next ? "The query returned " + (UBound($aResults)+1) + " results." ? Get $ Function fnADODBQuery($What,$From,$Where,Optional $OrderBy, Optional $Scope) Dim $objconnection, $objCommand, $objRecordSet If NOT $Scope $Scope = 2 EndIf If VarType($What)>8192 $sWhat=Join($What,',') Else $sWhat=$What EndIf $sQuery = "Select " + $sWhat + " from " + "'" + $From + "' where " + $Where + $OrderBy $objConnection = CreateObject("ADODB.Connection") $objCommand = CreateObject("ADODB.Command") $objConnection.Provider = "ADsDSOObject" $objConnection.Open("Active Directory Provider") $objCommand.ActiveConnection = $objConnection $objCommand.CommandText = $sQuery $objCommand.Properties("Page Size").Value = 1000 $objCommand.Properties("Timeout").Value = 30 $objCommand.Properties("Searchscope").Value = $Scope $objCommand.Properties("Cache Results").Value = NOT 1 $objRecordSet = $objCommand.Execute $objRecordSet.MoveFirst If VarType($What)>8192 For Each $Field in $What $GetValue = $GetValue + '$' + 'objRecordSet.Fields("' + $Field + '").Value,' Next $GetValue=Substr($GetValue,1,Len($GetValue)-1) Else $GetValue = '$' + 'objRecordSet.Fields("' + $What + '").Value' EndIf Dim $aRecords[0] $r = 0 Do $nul=Execute('$'+'aProperties = ' + $GetValue) $aRecords[$r] = $aProperties $objRecordSet.MoveNext $r = $r + 1 ReDim Preserve $aRecords[$r] Until $objRecordSet.EOF ReDim Preserve $aRecords[$r-1] $fnADODBQuery = $aRecords EndFunction
|
Top
|
|
|
|
#139144 - 2005-05-06 02:07 AM
Re: Find Computers using AD Site and Domain?
|
supermanzdead
Getting the hang of it
Registered: 2005-03-09
Posts: 50
|
interesting, I will have to try that out...
NTDOC: can you show me an example of pulling from an OU?
_________________________
~Mwah
|
Top
|
|
|
|
#139146 - 2005-05-08 07:49 PM
Re: Find Computers using AD Site and Domain?
|
supermanzdead
Getting the hang of it
Registered: 2005-03-09
Posts: 50
|
in $aWhat = "Name" what should go here?
_________________________
~Mwah
|
Top
|
|
|
|
#139148 - 2005-05-08 07:56 PM
Re: Find Computers using AD Site and Domain?
|
supermanzdead
Getting the hang of it
Registered: 2005-03-09
Posts: 50
|
I mean is there any particular name, or can it be anything?
_________________________
~Mwah
|
Top
|
|
|
|
#139152 - 2005-05-08 08:11 PM
Re: Find Computers using AD Site and Domain?
|
supermanzdead
Getting the hang of it
Registered: 2005-03-09
Posts: 50
|
for all LDAP query's do you have to make an ADO connection? Can you run a query and get results without?
_________________________
~Mwah
|
Top
|
|
|
|
#139156 - 2005-05-08 09:11 PM
Re: Find Computers using AD Site and Domain?
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
|
Here is a small example, but you can search the board here or look at the examples from Microsoft. A lot of them do not use ADO
Code:
$Group = GetObject("LDAP://CN=grfxe,ou=Groups,dc=mycompany,dc=com") For Each $member In $Group.members ? $member.adspath ? $member.fullname Next
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 373 anonymous users online.
|
|
|