'==========================================================================
'
' VBScript:  AUTHOR: Ed Wilson , MS,  6/26/2006
'
' NAME: <BasicQuery.vbs>
'ver. 2.0
' COMMENT: Key concepts are listed below:
'1.Makes connection into Active Directory using AdsDSOObject provider
'2.Uses an LDAP syntax query with ADO to query AD.
'3.Data is returned in a recordSet. Use the EOF property to determine
'4.When are at the end of the recordSet. Uses While NOT ... Wend to Loop
'5.Could just as easily use Do Until objRecordSet.EOF ... Loop instead.
'==========================================================================

Option Explicit    	    'Force the scripter to declare variables
On Error Resume Next 		'Go to the next line if it encounters an Error
Dim strQuery 						'Holds query to execute
Dim objConnection 			'Makes connection to active directory
Dim objCommand 					'The command object executes the query
Dim objRecordSet 				'Holds the data returned by the query

strQuery = "<LDAP://dc=nwtraders,dc=msft>;;name;subtree"
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = strQuery
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
    WScript.Echo objRecordSet.Fields("name")
    objRecordSet.MoveNext
Wend

objConnection.Close
