Find OUs (VBScript)

This code can be found in Chapter 5 of Active Directory Cookbook, 2nd edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

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

strBase = "<LDAP://<DomainDN>>"
strOUName = "Finance"
strFilter = "(&(objectCategory=organizationalUnit)" _
  "&(name=" & strOUName & "))"
strAttributes = "distinguishedName"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
  strName = objRecordSet.Fields("distinguishedName").Value
  Wscript.Echo "Distinguished Name: " & strName
  objRecordSet.MoveNext
Wend

objConnection.Close

This code has been viewed 3047 times.

New from the creators of TechTasks.com: StatSheet.com