Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having a terrible time with AD and computer description attribute 4

Status
Not open for further replies.

kmcferrin

MIS
Jul 14, 2003
2,938
US
Hello all,

I am writing a script that queries AD for all computers that are part of our domain, along with some additional attributes of the computer's AD object, then connects to that computer and looks for something (not relevant here), and outputs the results into several log different log files based on results. At any rate, everything works just fine except for displaying the computer's description attribute from AD. Here's some sample code of the problematic bit:

Code:
' Connects to AD and executes query
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection


objCommand.Properties("Sort On") = "CN"
objCommand.CommandText = "<LDAP://dc=mydomain,dc=com>;(objectCategory=computer);description,name,operatingSystem;subtree"
Set objRecordset = objCommand.Execute


Set objFile = objFSO.OpenTextFile(strLogFile, ForAppending)
WScript.Echo "Gathering data from Active Directory ..."
objFile.Writeline "Gathering data from Active Directory ..."
objFile.Close

objRecordSet.MoveFirst
intRecordCount = 0
intOnlineCount = 0
intOfflineCount = 0
intRXMInstall = 0
intUpgraded = 0

Do Until objRecordSet.EOF
    strComputer = objRecordSet.Fields("Name")
    strDescription = objRecordSet.Fields("description")
    If IsPingable(strComputer, "", "") Then
        strPingResult = "Online"
        intOnlineCount = intOnlineCount + 1
        strLogData = strComputer & " " & strPingResult & " - " & Now
        Set objFile = objFSO.OpenTextFile(strLogFile, ForAppending)
        Wscript.Echo strLogData
        objFile.WriteLine strLogData
        objFile.Close
        Wscript.Echo strComputer
        Wscript.Echo strDescription

When I run the script it is erroring out on the "Wscript.Echo strDescription" line with a type mismatch error. I can change it so that it displays the name, operatingSystem attributes, or most any other attribute. But it always hangs on the description. I have checked using ADSIEdit and the objects being returned do have descriptions attached, and the AD description attribute appears to be the field that has the data that I want (in AD Users and Computers we put make/model/serial number in the description field). If I change the last line to "Wscript.Echo objRecordSet.Fields("description")" I still get the type mismatch.

I suspect that for some reason the description attribute is returned in format other than the expected format, but I have no way to determine what it is returning. I keep seeing references to the Active Directory Programmer's Guide on MSDN, but all of the links to it that I have found appear to be dead. Is the description attribute returned as an array or something goofy like that?

Anyone have any ideas?
 
I think its should just be a string. Try retrieving the type in your script.

Do something like:

[script]

wscript.echo Type(objRecordSet.Fields("description"))

[/script]
 
I would suggest the VarType() or TypeName() functions (or both).

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
He is correct....I mistyped my answer...you need to try:

Code:
wscript.echo TypeName(objRecordSet.Fields("description"))
 
Interesting. TypeName() gets me Field for the name and description attributes. VarType() gets me an 8 for the name and an 8204 for the description. So a quick Google tells me that it [bold]is[/bold] an array after all, an array of variants.

OK, so if it is an array I would think that the first element is probably the piece of data/string that I am looking for. Now, how do I got about getting said element from this? It won't let me treat it as an array by tacking an element number on the back (0), I've tried shoehornging it into a collection, but no matter what I don't seem to be able to get any data out of it. Any other ideas?
 
And what about this ?
Wscript.Echo Join(strDescription)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Or something like:

For each item in strDescription
wscript.echo item.name
next

I am digging up some of my code that I have written before to see. I thought for sure that you could echo out the desc as a string with no problems. Try the above as a concept and see if it works.

 
i like PHV's join,
in the past i have used (where adVariant is something cool)
While Not oRS.EOF
For i = 0 To oRS.oFields.Count - 1
If oRS.Fields(i).Type = adVariant And Not (IsNull(oRS.Fields(i).Value)) Then
For j = LBound(oRS.FIelds(i).Value) To UBound(you guessed it)
strVal = strVal & oRS.Fields(i).Value(j)
Next
Else
strVal = oRS.Fields(i).Value
End If
Next
oRS.MoveNext
Wend

'please excuse the lack of tab'n
'
 
Aha!

Thanks for the tips. It kept mismatching when I was trying to assign a value to strDescription, but I took a hint from PHV and tried:

Code:
    strDescription = Join(objRecordSet.Fields("description"))
    Wscript.Echo strDescription

And it worked like a charm. Thank you so much for all of your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top