Page 1 of 2 12>
Topic Options
#139140 - 2005-05-04 04:15 PM Find Computers using AD Site and Domain?
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
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
_________________________
~Mwah

Top
#139141 - 2005-05-04 07:08 PM Re: Find Computers using AD Site and Domain?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
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.
Top
#139142 - 2005-05-04 10:31 PM Re: Find Computers using AD Site and Domain?
Chris S. Offline
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. Offline
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 Offline
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
#139145 - 2005-05-06 02:18 AM Re: Find Computers using AD Site and Domain?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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"
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#139146 - 2005-05-08 07:49 PM Re: Find Computers using AD Site and Domain?
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
in $aWhat = "Name" what should go here?
_________________________
~Mwah

Top
#139147 - 2005-05-08 07:55 PM Re: Find Computers using AD Site and Domain?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#139148 - 2005-05-08 07:56 PM Re: Find Computers using AD Site and Domain?
supermanzdead Offline
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
#139149 - 2005-05-08 07:59 PM Re: Find Computers using AD Site and Domain?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
It can be anything you want provided it is a suported value. Is there something besides "Name" that you want?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#139150 - 2005-05-08 08:00 PM Re: Find Computers using AD Site and Domain?
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
nah was just curious
_________________________
~Mwah

Top
#139151 - 2005-05-08 08:03 PM Re: Find Computers using AD Site and Domain?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
An example from Chris' UDF:
$aWhat = "Name", "givenName", "sn", "displayName", "telephoneNumber", "AdsPath"
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#139152 - 2005-05-08 08:11 PM Re: Find Computers using AD Site and Domain?
supermanzdead Offline
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
#139153 - 2005-05-08 08:47 PM Re: Find Computers using AD Site and Domain?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Yes you can query without using ADO, however ADO gives you more control of the query
Top
#139154 - 2005-05-08 08:52 PM Re: Find Computers using AD Site and Domain?
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
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
_________________________
~Mwah

Top
#139155 - 2005-05-08 09:09 PM Re: Find Computers using AD Site and Domain?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
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.
_________________________
There are two types of vessels, submarines and targets.

Top
#139156 - 2005-05-08 09:11 PM Re: Find Computers using AD Site and Domain?
NTDOC Administrator Offline
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
#139157 - 2005-05-08 09:27 PM Re: Find Computers using AD Site and Domain?
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
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
_________________________
~Mwah

Top
#139158 - 2005-05-08 11:12 PM Re: Find Computers using AD Site and Domain?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
The ADODB method is so much faster than DOC's way, especially since he doesn't use a filter.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#139159 - 2005-05-08 11:16 PM Re: Find Computers using AD Site and Domain?
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
Well I figure once I get the basic query without ADO down I can start learning ADODB, I already started reading up on both
_________________________
~Mwah

Top
Page 1 of 2 12>


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 898 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.073 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

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