Very slow finding a user in a Group AD VBS

231 Views Asked by At

when I try to find a user if is a member of a group it takes too long. Will it be possible to filter base DN for the LDAP search?

Here is the function.

' *****************************************************
'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
' *****************************************************

Thank you

1

There are 1 best solutions below

2
On BEST ANSWER

You don't need to go through all groups once you have found the matching group, this should help:

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

Also, there's no need to create and add group names to a Dictionary.