Re: LDAP query information



Vishn wrote:

It takes the distinguishedName attribute but does not compute or display
in
the query result. Same with most of the fields of the "computer' object
attributes(Checked with most of the 'computer' object attributes in AD
Schema).
Is there any way by which we can pull ' Canonical name of the object'
attribute for the 'Computer' object. maybe a VBScript if not a LDAP query?
A VBScript which may check the computer accounts in a domain from AD
directly & compute the result with all the attributes of the 'Computer'
object or get the list of computers from a text file & then query AD &
compute the results.
Can you please construct a VBScript for me if thats the only solution?
That seems to be my last hope :(
Thanks for your help!

Hi,

The following VBScript program uses ADO to retrieve many of the attributes
of all computer objects in the domain. It should be run at a command prompt
using cscript. The values are output delimited by semicolons. The output can
be redirected to a text file and imported into a spread***:
=====================
Option Explicit

Dim objRootDSE, strDNSDomain, adoCommand, adoConnection
Dim strBase, strFilter, strAttributes, strQuery, adoRecordset
Dim strNTName, lngUSN, arrCanonical, strCanonical
Dim strDN, strDesc, strRole, strManaged, strOS, strOSVer
Dim strSP, strLocation, strCreated, strChanged

' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE";)
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Search entire domain.
strBase = "<LDAP://"; & strDNSDomain & ">"

' Filter on computer object.
strFilter = "(objectCategory=computer)"

' Comma delimited list of attributes.
strAttributes = "sAMAccountName,canonicalName,distinguishedName," _
& "description,machineRole,managedBy,operatingSystem," _
& "location,operatingSystemVersion,operatingSystemServicePack," _
& "whenCreated,whenChanged"

' Construct LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

' Execute the query.
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute

' Enumerate the recordset and display values.
Do Until adoRecordset.EOF
strNTName = adoRecordset.Fields("sAMAccountName").Value
arrCanonical = adoRecordset.Fields("canonicalName").Value
strCanonical = arrCanonical(0)
strDN = adoRecordset.Fields("distinguishedName").Value
strDesc = adoRecordset.Fields("description").Value
strRole = adoRecordset.Fields("machineRole").Value
strManaged = adoRecordset.Fields("managedBy").Value
strLocation = adoRecordset.Fields("location").Value
strOS = adoRecordset.Fields("operatingSystem").Value
strOSVer = adoRecordset.Fields("operatingSystemVersion").Value
strSP = adoRecordset.Fields("operatingSystemServicePack").Value
strCreated = adoRecordset.Fields("whenCreated").Value
strChanged = adoRecordset.Fields("whenChanged").Value
Wscript.Echo strNTName & ";" & strCanonical & ";" & strDN _
& ";" & strDesc & ";" & strRole & ";" & strManaged _
& ";" & strLocation & ";" & strOS & ";" & strOSVer _
& ";" & strSP & ";" & strCreated & ";" & strChanged
adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close

' Clean up.
Set objRootDSE = Nothing
Set adoCommand = Nothing
Set adoConnection = Nothing
Set adoRecordset = Nothing

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net


.