From: redwheeler on
The following script pulls the User Name from one OU but I have ten
OUs that I want to pull this same information from and I am unable to
figure out a way to have the script loop through the other 9 OUs. The
OU=City part is what changes. Can anyone suggest a way to do this?

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2
Const ForAppending = 8
Const ForReading = 1
Dim fso, f
Text1 = "OU=Users,OU=City,OU=Division,dc=Mycompany,dc=local' WHERE
objectCategory='user'"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\testfile.txt", ForAppending, True)

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE



strQuery = "SELECT Name FROM 'LDAP://" & Text1

objCommand.CommandText = strQuery

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF

f.writeline objRecordSet.Fields("Name").Value

objRecordSet.MoveNext
Loop

f.Close


objFile.Close

From: Richard Mueller [MVP] on

<redwheeler(a)gmail.com> wrote in message
news:1184852282.431978.283520(a)x40g2000prg.googlegroups.com...
> The following script pulls the User Name from one OU but I have ten
> OUs that I want to pull this same information from and I am unable to
> figure out a way to have the script loop through the other 9 OUs. The
> OU=City part is what changes. Can anyone suggest a way to do this?
>
> On Error Resume Next
>
> Const ADS_SCOPE_SUBTREE = 2
> Const ForAppending = 8
> Const ForReading = 1
> Dim fso, f
> Text1 = "OU=Users,OU=City,OU=Division,dc=Mycompany,dc=local' WHERE
> objectCategory='user'"
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set f = fso.OpenTextFile("C:\testfile.txt", ForAppending, True)
>
> Set objConnection = CreateObject("ADODB.Connection")
> Set objCommand = CreateObject("ADODB.Command")
> objConnection.Provider = "ADsDSOObject"
> objConnection.Open "Active Directory Provider"
>
> Set objCommand.ActiveConnection = objConnection
> objCommand.Properties("Page Size") = 1000
> objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
>
>
>
> strQuery = "SELECT Name FROM 'LDAP://" & Text1
>
> objCommand.CommandText = strQuery
>
> Set objRecordSet = objCommand.Execute
>
> objRecordSet.MoveFirst
>
> Do Until objRecordSet.EOF
>
> f.writeline objRecordSet.Fields("Name").Value
>
> objRecordSet.MoveNext
> Loop
>
> f.Close
>
>
> objFile.Close
>

Here's another suggestion. You can hard code the OU names (actually, part of
the OU names) in an array:

arrOUs = Array("City", "East", "West", "North", "South")
For Each strOU in arrOUs
Text1 = "OU=Users,OU= " & strOU & ",OU=Division,dc=Mycompany,dc=local'
WHERE objectCategory='user'"
' Continue...
Next

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--


 | 
Pages: 1
Prev: Execute VBS from Notepad++
Next: File in Use