Thursday, June 14, 2012

AD Script to list OUs

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

AD Script to list Sites, Descriptions

Recently, I had to list and dump the sites information from AD. So, I created this script for it:

on error resume next

Set objRootDSE = GetObject("LDAP://RootDSE")
strConfigurationNC = objRootDSE.Get("configurationNamingContext")

strSitesContainer = "LDAP://cn=Sites," & strConfigurationNC
Set objSitesContainer = GetObject(strSitesContainer)
objSitesContainer.Filter = Array("site")
intCtr = 0
For Each objSite In objSitesContainer
    intStart = Instr(objSite.description,"(")
    intEnd = Instr(objSite.description,")")
    'Wscript.echo objSite.description
    strSiteCode = Mid(objSite.description,intStart)

    intStart = Instr(objSite.description,":")
    intEnd = Instr(objSite.description,",")
    strBusiness = Mid(objSite.description,intStart+1, intEnd-intStart-1)

    intStart = InstrRev(objSite.description,"/")
    intEnd = InstrRev(objSite.description,":")
    strLocation = Mid(objSite.description,intStart+1, intEnd-intStart-1)

    intStart = Instr(objSite.description,"/")
    intEnd = InstrRev(objSite.description,"/")
    strCountry = Mid(objSite.description,intStart+1, intEnd-intStart-1)

    WScript.Echo strSiteCode & "-" & strLocation & ";" & objSite.cn &  ";" & strCountry & ";" & strBusiness
    intCtr = intCtr + 1
    'If intcTR = 15 Then Exit For
Next

You need to run in on a DC.