问题
I have a script that shows a filtered list of what groups a particular AD user is a member of. It works perfectly fine as a VBS file, but when imported into either an HTA or HTML file it gives me a "The search filter cannot be recognized" error message when running the "objRecordSet.MoveFirst" line below.
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
I also have another script which is nearly identical that does work in HTA/HTML format. This one just shows where an email account is forwarded if at all:
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
I can't seem to find any functional differences between each script's "objRecordSet.MoveFirst" line.
Please help!
Edit:
Same results here. Tried an alternate chunk of code to perform the same actions - works fine as VBS but not 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
回答1:
If you're on a 64-bit OS, try running the 32-bit MSHTA.exe from a command line:
C:\Windows\System32\mshta.exe C:\YOUR_PATH\yourscript.hta
I've had problems with the 64-bit version (C:\Windows\SysWOW64\mshta.exe) failing to support COM/ActiveX interfaces. Windows 7 will open .HTA files with the 64-bit version by default.
回答2:
In your HTA, are you putting your code in side a sub or function?
<script language = "VBScript">
Sub Window_Onload
##your code here##
End Sub
</script>
If not, it could be messing up in trying to Dim USER as a global variable.
Also perfer to rewrite the Echo to something like
TextOut.innerHTML = GroupList
</script>
<html><body>
<div id="TextOut"></div>
来源:https://stackoverflow.com/questions/11527520/script-works-as-vbs-but-not-html