VBScript错误访问Active Directory

时间:2014-03-31 03:47:07

标签: vbscript active-directory

好的,我投降了。我正在编写来自Active Directory的一些数据的脚本,而且我遇到了一个我无法弄清楚的错误。我的剧本是;

'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

我收到C:\ Bin \ SecurityGroupt.vbs(26,2)的错误Microsoft VBScript运行时错误:类型不匹配

这就行了;

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

使用Sysinternals的Active Directory Explorer,我看到名为description的值,它说它是DirectoryString。我尝试使用该值进行的任何操作,将其视为字符串,都会出现此错误。我试着把它变成一个字符串并得到同样的东西。

一定有我遗失的东西。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

而不是

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

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

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

!!!在最后一种情况下,不要忘记事先定义strDescription变量:

Dim strDescription

答案 1 :(得分:0)

如果该字段包含数组,则可以Join数组元素:

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

答案 2 :(得分:0)

这是很长一段时间,但以防万一......

请改为尝试:

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

有更简洁的方法可以做到,但我是在记忆中工作。