简体   繁体   English

VBScript错误访问Active Directory

[英]VBScript Error Accessing Active Directory

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. 我正在编写来自Active Directory的一些数据的脚本,但遇到了一个我不知道的错误。 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 我收到C:\\ Bin \\ SecurityGroupt.vbs(26,2)的错误Microsoft VBScript运行时错误:类型不匹配

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. 使用Sysinternals的Active Directory Explorer,我看到名为description的值,它说它是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? 有什么建议么?

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: 在最后一种情况下,请不要忘记预先定义strDescription变量:

Dim strDescription

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

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

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. 有更清洁的方法可以执行此操作,但是我正在记忆中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM