
enumerating membership of groups by the user's token
Quote:
>Hello,
>Does anyone know if there is a way to enumerate the
>membership of a W2K user in VBS based on the contents of
>their token? This way I will get nested membership
>without having to check thousands of groups. I have seen
>implementations of this in c++ but I want to do it in VBS
>is possible.
Hi,
I assume you refer to the "TokenGroups" attribute. Here is
code for an IsMember function you can modify as desired.
The dictionary object oGroupList is Dim'd so it will be
global.
Option Explicit
Dim oUser, sGroup, oGroupList
' Bind to the user object in Active Directory.
Set oUser = GetObject
("LDAP://cn=TestUser,ou=Sales,dc=MyDomain,dc=com")
sGroup = "Students"
If IsMember(sGroup) Then
MsgBox "User is a member of group " & sGroup
Else
MsgBox "User is NOT a member of group " & sGroup
End If
Function IsMember(sGroup)
' Function to test for group membership.
' sGroup is the sAMAccountName of the group to test.
' oGroupList is a dictionary object, with global scope.
' Returns True if the user or computer is a member.
If IsEmpty(oGroupList) Then
Call LoadGroups
End If
IsMember = oGroupList.Exists(sGroup)
End Function
Sub LoadGroups
' Subroutine to populate dictionary object with groups.
' oUser is the user or computer object, with global scope.
' oGroupList is a dictionary object, with global scope.
Dim hUserGroups, j
Dim sUserGroupSid(), oGroup
Set oGroupList = CreateObject("Scripting.Dictionary")
oGroupList.CompareMode = vbTextCompare
oUser.GetInfoEx Array("TokenGroups"), 0
hUserGroups = oUser.Get("TokenGroups")
ReDim sUserGroupSid(UBound(hUserGroups))
For j = 0 To UBound(hUserGroups)
sUserGroupSid(j) = OctetToHexStr(hUserGroups(j))
Set oGroup = GetObject("LDAP://<SID=" _
& sUserGroupSid(j) & ">")
oGroupList(oGroup.sAMAccountName) = True
Next
Set oGroup = Nothing
End Sub
Function OctetToHexStr(hOctet)
' Function to convert OctetString (byte array)
' to Hex string.
Dim k
OctetToHexStr = ""
For k = 1 To Lenb(hOctet)
OctetToHexStr = OctetToHexStr & Right("0" _
& Hex(Ascb(Midb(hOctet, k, 1))), 2)
Next
End Function
Richard