strOUBase = WScript.Arguments.Named("ou")
Set objConn = CreateObject("ADODB.Connection")
Set objComm = CreateObject("ADODB.Command")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
Set objComm.ActiveConnection = objConn
objComm.Properties("Page Size") = 1000
objComm.Properties("Searchscope") = 2
objComm.Properties("Sort on") = "name"
objComm.CommandText = "SELECT name,distinguishedName From 'LDAP://" & strOUBase _
& "' WHERE objectClass='computer'"
Set objRS = objComm.Execute
If objRS.RecordCount > 0 Then
objRS.MoveFirst
WScript.Echo "Machine Name,OU,Domain,OS"
Do Until objRS.EOF
strDN = Replace(objRS.Fields("distinguishedName").Value,"/","\/")
Set objPC = GetObject("LDAP://" & strDN)
arrCNInfo = Split(getCNFromDN(strDN),"/")
strName = arrCNInfo(UBound(arrCNInfo))
strDomain = arrCNInfo(0)
For i = 1 To UBound(arrCNInfo)-1
strOU = strOU & arrCNInfo(i) & "/"
Next
If Right(strOU,1) = "/" Then strOU = Left(strOU,Len(strOU)-1)
strOS = objPC.operatingSystem
WScript.Echo strName & "," & strOU & "," & strDomain & "," & strOS
strOU = ""
objRS.MoveNext
Loop
End If
Function getCNFromDN(strDN)
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_1779 = 1
Const ADS_NAME_TYPE_CANONICAL = 2
Set objNT = CreateObject("NameTranslate")
objNT.Init ADS_NAME_INITTYPE_GC, ""
objNT.Set ADS_NAME_TYPE_1779, strDN
getCNFromDN = objNT.Get(ADS_NAME_TYPE_CANONICAL)
End Function
|