I created this script to list out all the OUs and count of computers in each OU. You need to run it on a DC, and replace XXX with complete distinguished DC name:
'On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
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://xxxx/OU=xx,OU=xx,DC=xx,DC=xx' WHERE objectCategory='organizationalUnit'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Wscript.echo "OU,Count of Computers"
intCtr = 0
Do Until objRecordSet.EOF
Set objOU = GetObject(objRecordSet.Fields("ADsPath").Value)
'Wscript.Echo objOU.distinguishedName
If Instr(objOU.distinguishedName,"T_Workstations") Then
Continue
End If
objOU.Filter = Array("Computer")
intCtr=0
For Each objItem in objOU
'Wscript.Echo objItem.CN
intCtr = intCtr + 1
Next
Wscript.Echo objOU.distinguishedName & "," & intCtr
objRecordSet.MoveNext
Loop
'On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
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://xxxx/OU=xx,OU=xx,DC=xx,DC=xx' WHERE objectCategory='organizationalUnit'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Wscript.echo "OU,Count of Computers"
intCtr = 0
Do Until objRecordSet.EOF
Set objOU = GetObject(objRecordSet.Fields("ADsPath").Value)
'Wscript.Echo objOU.distinguishedName
If Instr(objOU.distinguishedName,"T_Workstations") Then
Continue
End If
objOU.Filter = Array("Computer")
intCtr=0
For Each objItem in objOU
'Wscript.Echo objItem.CN
intCtr = intCtr + 1
Next
Wscript.Echo objOU.distinguishedName & "," & intCtr
objRecordSet.MoveNext
Loop
No comments:
Post a Comment