在 Group AD VBS 中查找用户非常慢

Very slow finding a user in a Group AD VBS

当我尝试查找某个用户(如果是某个组的成员)时,花费的时间太长。是否可以为 LDAP 搜索过滤基本 DN?

这是函数。

' *****************************************************
'This function checks if the given AD user is a member of the given group.
Function IsMember(domainName,userName,groupName)
   Set groupListD = CreateObject("Scripting.Dictionary")
   groupListD.CompareMode = 1
   ADSPath = domainName & "/" & userName

   Set objUser = GetObject("WinNT://" & ADSPath & ",user")
   For Each objGroup in objUser.Groups
       groupListD.Add objGroup.Name, "-"
   Next
   IsMember = CBool(groupListD.Exists(groupName))
   
End Function
' *****************************************************

谢谢

找到匹配组后,您无需浏览所有组,这应该会有所帮助:

Function IsMember(domainName, userName, groupName)
    Dim sADSPath
    Dim objUser
    Dim objGroup

    sADSPath = domainName & "/" & userName
    
    Set objUser = GetObject("WinNT://" & sADSPath & ",user")
   
    If objUser Is Nothing Then 
        IsMember = False
        Exit Function
    End If 

    For Each objGroup In objUser.Groups
        If StrComp(objGroup.Name, groupName, vbTextCompare) = 0 Then
            IsMember = True
            Exit Function
        End If
    Next
   
    IsMember = False
   
End Function

此外,无需创建群组名称并将其添加到词典。