0
votes

OK, I surrender. I'm working on scripting some data from Active Directory and I've hit a bug I just can't figure out. My script is;

'On Error Resume Next
Option Explicit

dim objConnection
dim objCommand
dim objRecordset

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 Name, description, distinguishedName, member FROM     'LDAP://ou=mybusiness,dc=huntfamily,dc=local' WHERE objectCategory='group'"  
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields("Name").Value  '   & "," & _
    Wscript.Echo objRecordSet.Fields("description").Value
    Wscript.Echo objRecordSet.Fields("distinguishedName").Value
Wscript.Echo objRecordSet.Fields("member").Value

    objRecordSet.MoveNext
Loop

I"m getting an error of C:\Bin\SecurityGroupt.vbs(26, 2) Microsoft VBScript runtime error: Type mismatch

This is on the line;

Wscript.Echo objRecordSet.Fields("description").Value

Using Active Directory Explorer from Sysinternals, I see the value called description and it says it is a DirectoryString. Anything that I try to do with that value, treating it as a string, gives this error. I tried casting it to a string and got the same thing.

There must be something I am missing. Any suggestions?

3
What's the output of WScript.Echo TypeName(objRecordSet.Fields("description").Value)? - Ansgar Wiechers

3 Answers

0
votes

Instead of

Wscript.Echo objRecordSet.Fields("Description").Value

try

If (isnull(objRecordSet.Fields("Description").Value)) Then
  Wscript.Echo "Description not defined"
Else
  Wscript.Echo objRecordSet.Fields("Description").Value
End If

or

strDescription = objRecordSet.Fields("Description").Value
If IsNull( strDescription) Then
  Wscript.Echo "Description not defined"
Else
  Wscript.Echo strDescription
End If

!!! in the last case do not forget to define strDescription variable in advance:

Dim strDescription
0
votes

If the field contains an array you could Join the array elements:

WScript.Echo Join(objRecordSet.Fields("description").Value, "")
0
votes

It's been a long time but just in case...

Try this instead:

On Error Resume Next

Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields("Name").Value  '   & "," & _
    If Err.Number = 0 Then
        WScript.Echo "Value not defined"
        Err.Clear
    End If
    Wscript.Echo objRecordSet.Fields("description").Value
    If Err.Number = 0 Then
        WScript.Echo "Value not defined"
        Err.Clear
    End If
    Wscript.Echo objRecordSet.Fields("distinguishedName").Value
    If Err.Number = 0 Then
        WScript.Echo "Value not defined"
        Err.Clear
    End If
    Wscript.Echo objRecordSet.Fields("member").Value
    If Err.Number = 0 Then
        WScript.Echo "Value not defined"
        Err.Clear
    End If
    objRecordSet.MoveNext       
Loop

On Error Goto 0

There are cleaner ways to do it but I am working from memory.