Re: LDAP query information



I ran the script & got one more type mismatch error:

C:\Richardscript>cscript //nologo queryAD.vbs >output.txt
C:\Richardscript\queryAD.vbs(52, 6) Microsoft VBScript runtime error: Type
misma
tch

C:\Richardscript>

Error mismatch on the 52nd line which is
strDesc = adoRecordset.Fields("description").Value & ""
The 51st line was
strDN = adoRecordset.Fields("distinguishedName").Value
So, I suppose the DistinguisedName attribute worked fine(No error msgs)
Do I have to put the If -- End if condition for this one too? If so, what
about the declaration part. If I get the same error msg on the other lines,
Do I have to do the same conditional statements. Apologise for my ignorance.
What does the //nologo switch do? Will it make a difference in the
execution of the vbscript?

Thanks for your help!

"Richard Mueller" wrote:

Hi,

The error message indicates the line number in the script, in this case 57.
Most likely there is a missing value, which results in a Null, which causes
the type mismatch. The way I fix this is to append a blank string, "", to
the values retrieved. For example:
================
Do Until adoRecordset.EOF
strNTName = adoRecordset.Fields("sAMAccountName").Value
arrCanonical = adoRecordset.Fields("canonicalName").Value
If (TypeName(arrCanonical) = "Variant()") Then
strCanonical = arrCanonical(0)
Else
strCanonical = ""
End If
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
============
The sAMAccountName is required so cannot be missing. The canonicalName
attribute is bit strange. It is multi-valued, although I think there is
always only one value. It is retrieved by ADO as an array. Just to be safe,
I check if it is a variant array above. I hope this helps.

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

"Vishn" <Vishn@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F4CE4254-3A67-4533-9F54-618E9858DB65@xxxxxxxxxxxxxxxx
Hi Richard,
Can you please help me with the below error message I received when I
executed the VBscipt which you provided?

Appreciate your help!


"Vishn" wrote:


It gave me the below error message when I ran the script.I suppose its
some
issue with the Type declaration.
--------------------------------------------------
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

O:\>c:

C:\>cd compaccts

C:\compaccts>cscript compaccts.vbs

C:\compaccts>cscript //nologo compaccts.vbs >report.txt
C:\compaccts\compaccts.vbs(57, 5) Microsoft VBScript runtime error: Type
mismatc
h

C:\compaccts>
------------------------------------------------------------------------
This is how I had saved the script
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


Thanks for your help!

"Richard Mueller" wrote:

Hi,

Yes, the script uses ADO to query AD directly, which is very
efficient - no
need to bind to each computer object. If the program is saved in a file
called Computers.vbs, run it at a command prompt with the following
command
to redirect the output to a text file:

cscript //nologo Computers.vbs > report.txt

If Computers.vbs is not in the current directory, include the path to
the
file. The "//nologo" parameter suppresses WSH info. You can add or
remove
attributes, change the order the values are output, use a different
delimiter, whatever.

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

"Vishn" <Vishn@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:AE498841-CBE0-4969-AE04-AE2EBE005387@xxxxxxxxxxxxxxxx
Hi,
WOW! That was quite fast. You are the best!!.
I ran the script, it executed succesfully in cmd prompt. Does
it
query the AD directly? How can we redirect the AD results to a text
file(create a text file in the same directory as the vbs file &
then),I
mean
the cmds which have to be added to the VBScript to redirect the
output.
The
script looks like there is no input file required,so it should be
querying
AD
directly when executed.Please correct me I am wrong. Is there a way
where
in
it can accept input from a text file, run this script which will
query the
AD
& redirect the results to an output text file?
Thank you so much for taking your time in helping me out with this
issue.
Really appreciate your help!




"Richard Mueller" wrote:

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
.