Freigeben über


Script to List out OU’s and objects within it.

I thought of sharing a script, which will list out a report for all the OU’s in domain. This report is very handy if you want to find out how many objects are part of each OUs. This script generates the report.

In the following code, replace the name of your domain at the place of striked out character in Distinguished name format.

CODE:

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2
Dim I
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

objCommand.CommandText = _
"SELECT ADsPath FROM 'LDAP://dc=2003dom,dc=local' WHERE " & _
"objectCategory='organizationalUnit'" 

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
Set objOU = GetObject(objRecordSet.Fields("ADsPath").Value)
I=0
Wscript.Echo objOU.distinguishedName
For Each objItem in objOU
I=I+1
Wscript.Echo " ",objItem.distinguishedName
Next
Wscript.Echo "Total Objects in this OU:",I
objRecordSet.MoveNext
Loop

The output will be something like this:

image