supermanzdead
(Getting the hang of it)
2005-05-04 04:15 PM
Find Computers using AD Site and Domain?

Right now I am pulling up a list of computers using the following code:

Code:

$objDomain = getobject("WinNT://ourwonderfuldomain")



I was wondering how (and if) I could pull a list of computers from an Windows 2000 Active Directory using the above code along with Site. I am hoping that could pull the list faster than what I am using now which is:

Code:

$objDomain.filter = "computer",""
For Each $computer in $objDomain
If Left($computer.name, 2) = "ATL"
$_ = $lstListView.items.add($computer.name)
Endif
Next



Thanks for any feedback


NTDOCAdministrator
(KiX Master)
2005-05-04 07:08 PM
Re: Find Computers using AD Site and Domain?

For the DOMAIN that code is probably just as well as some of the other examples. However you can pull from specific OUs if wanted instead of the entire Domain.

Chris S.
(MM club member)
2005-05-04 10:31 PM
Re: Find Computers using AD Site and Domain?

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



Chris S.
(MM club member)
2005-05-04 10:43 PM
Re: Find Computers using AD Site and Domain?

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



supermanzdead
(Getting the hang of it)
2005-05-06 02:07 AM
Re: Find Computers using AD Site and Domain?

interesting, I will have to try that out...

NTDOC: can you show me an example of pulling from an OU?


Les
(KiX Master)
2005-05-06 02:18 AM
Re: Find Computers using AD Site and Domain?

Just change the query:
$sFrom = "LDAP://DC=your,DC=domain,DC=com"

to:
$sFrom = "LDAP://OU=bla,OU=yada,DC=your,DC=domain,DC=com"


supermanzdead
(Getting the hang of it)
2005-05-08 07:49 PM
Re: Find Computers using AD Site and Domain?

in $aWhat = "Name" what should go here?

Les
(KiX Master)
2005-05-08 07:55 PM
Re: Find Computers using AD Site and Domain?

Depends on "What" you are wanting. My Carnac hat doesn't work since Johnny died but even without it, my guess is that "Name" is what you want.

supermanzdead
(Getting the hang of it)
2005-05-08 07:56 PM
Re: Find Computers using AD Site and Domain?

I mean is there any particular name, or can it be anything?

Les
(KiX Master)
2005-05-08 07:59 PM
Re: Find Computers using AD Site and Domain?

It can be anything you want provided it is a suported value. Is there something besides "Name" that you want?

supermanzdead
(Getting the hang of it)
2005-05-08 08:00 PM
Re: Find Computers using AD Site and Domain?

nah was just curious

Les
(KiX Master)
2005-05-08 08:03 PM
Re: Find Computers using AD Site and Domain?

An example from Chris' UDF:
$aWhat = "Name", "givenName", "sn", "displayName", "telephoneNumber", "AdsPath"


supermanzdead
(Getting the hang of it)
2005-05-08 08:11 PM
Re: Find Computers using AD Site and Domain?

for all LDAP query's do you have to make an ADO connection? Can you run a query and get results without?

NTDOCAdministrator
(KiX Master)
2005-05-08 08:47 PM
Re: Find Computers using AD Site and Domain?

Yes you can query without using ADO, however ADO gives you more control of the query

supermanzdead
(Getting the hang of it)
2005-05-08 08:52 PM
Re: Find Computers using AD Site and Domain?

I have been searching for examples, can you show me a small simple example of both? I am trying to learn...learning curve on this seems to be broken for me

Sealeopard
(KiX Master)
2005-05-08 09:09 PM
Re: Find Computers using AD Site and Domain?

Learning does imply trying for yourself, reading tha appropriate manuals, and experimenting. Thus, try the code, see wat happens if you supply different parameters and try to understand what the code does.

NTDOCAdministrator
(KiX Master)
2005-05-08 09:11 PM
Re: Find Computers using AD Site and Domain?

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


 


supermanzdead
(Getting the hang of it)
2005-05-08 09:27 PM
Re: Find Computers using AD Site and Domain?

Ah, I do read and try code...i have been stumped on the LDAP deal, I use simple examples to help myself learn...think I can understand how to do it without ADO with NTDOC's example, thanks

Les
(KiX Master)
2005-05-08 11:12 PM
Re: Find Computers using AD Site and Domain?

The ADODB method is so much faster than DOC's way, especially since he doesn't use a filter.

supermanzdead
(Getting the hang of it)
2005-05-08 11:16 PM
Re: Find Computers using AD Site and Domain?

Well I figure once I get the basic query without ADO down I can start learning ADODB, I already started reading up on both

NTDOCAdministrator
(KiX Master)
2005-05-09 12:28 AM
Re: Find Computers using AD Site and Domain?

Now Now... I didn't say it was "my way", he simply asked if it could be done and the answer is yes it can be done.

However in many cases there is no need or speed increase by using ADO depending upon the query. For a quickie this type of call will work fine. However the ADO method does give greater speed and control on larger or multi-object queries.