脚本作为VBS而不是HTML

时间:2012-07-17 17:15:23

标签: html vbscript active-directory hta

我有一个脚本,显示特定AD用户所属的组的筛选列表。它作为VBS文件工作完全正常,但是当导入HTA或HTML文件时,它会给我一个"搜索过滤器无法被识别" 运行下面的" objRecordSet.MoveFirst" 行时出现错误消息。

Dim User
Dim DIA
Dim GroupList

DIA = "No"
User = "UserNic"

Const ADS_SCOPE_SUBTREE = 2
Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D

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 memberOf FROM 'LDAP://dc=company,dc=com' WHERE objectCategory='user' And mailnickname='" & User & "'" 
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    arrMemberOf = objRecordSet.Fields("memberOf")
        if isArray(objRecordSet.Fields("memberOf")) Then
            For Each x in arrMemberOf
                If InStr(x,"GroupFilter") <> 0 Then
                    Group = x
                    Group = Right(Group,Len(Group)-3)
                    Group = Left(Group,InStr(Group,",")-1)
                    GroupList = Group & vbCrLf & GroupList
                End If
                If InStr(x,"DIA") <> 0 Then DIA = "Yes"
            Next
        End if
    objRecordSet.MoveNext
Loop

WScript.Echo GroupList
WScript.echo "DIA: " & DIA

我还有另一个脚本几乎相同, 以HTA / HTML格式工作。这个只显示电子邮件帐户转发的位置:

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, altRecipient FROM 'LDAP://dc=company,dc=com' WHERE objectCategory='user' And Name='*" & Hosp & "' And altRecipient='*'" 
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Count = 0
Do Until objRecordSet.EOF
    Name(Count) = objRecordSet.Fields("Name").Value
    Forward(Count) = objRecordSet.Fields("altRecipient").Value

    arrLines = Split(Forward(Count),",")
    search = Filter(arrLines,"CN=",True,1)
    for each x in search
        Forward(Count) = x
    Next
    Forward(Count) = Replace(Forward(Count),"CN=","")

    objRecordSet.MoveNext
    Count = Count + 1
Loop

我似乎无法找到每个脚本的&#34; objRecordSet.MoveFirst&#34; 行之间的任何功能差异。

请帮忙!

修改

这里的结果相同。尝试了另一块代码来执行相同的操作 - 可以像VBS一样正常但不能HTA:

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "<LDAP://dc=domain,dc=com>;" & "(&(objectCategory=Person)(mailnickname=" & User & "));" & "distinguishedName,Name;subtree"
Set objRecordSet = objCommand.Execute

intCount = 0
If objRecordSet.EOF Then
    Set WshShell = CreateObject("WScript.Shell")
    message = WshShell.Popup ("Unable to find a user with the alias '" & User & "'! Please try again...",, "programname", 0 + 16)
    'Exit Sub
Else
    While NOt objRecordSet.EOF
        intCount = intCount + 1
        objRecordSet.MoveNext
    WEND
        If intCount = 1 Then
            objRecordSet.MoveFirst
            Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedname"))
            Set colGroups = objUser.Groups
            For Each objGroup in colGroups
                Group = objGroup.CN
                GroupList = Group & vbCrLf & GroupList
            Next
        End If
End If

WScript.Echo GroupList

2 个答案:

答案 0 :(得分:0)

如果您使用的是64位操作系统,请尝试从命令行运行32位MSHTA.exe:

C:\Windows\System32\mshta.exe C:\YOUR_PATH\yourscript.hta

我遇到64位版本( C:\ Windows \ SysWOW64 \ mshta.exe )无法支持COM / ActiveX接口的问题。默认情况下,Windows 7将使用64位版本打开 .HTA 文件。

答案 1 :(得分:0)

在您的HTA中,您是将代码放在子或函数旁边吗?

<script language = "VBScript">
Sub Window_Onload
 ##your code here##
End Sub
</script>

如果没有,可能会在尝试将Dim USER作为全局变量时搞乱。

还可以将Echo重写为类似

的内容
TextOut.innerHTML = GroupList
</script>
<html><body>
<div id="TextOut"></div>