Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active Directory

Status
Not open for further replies.

bitwise

Programmer
Mar 15, 2001
269
US
Does anybody know how to find out if a given USER is in a give GROUP in Active Directory using VBScript?

Thanks,
bitwise
 
Set oHeaven = GetObject("WinNT://MyDomain,MyGroup")
MsgBox oHeaven.IsMember("WinNT://MyDomain,MyUser") Jon Hawkins
 
Actually, more accurately this is what you want:

function IsUserInGroup(strUser, strGroup)
Dim strDomain, objGroup, objUser
strDomain = "DOMAIN"

Set objGroup = GetObject("WinNT://" & strDomain & "/" & strGroup & ",group")
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
IsUserInGroup = objGroup.IsMember(objUser.ADsPath)
end function

Thanks,
-bitwise
 
Sorry, I posted as I was walking out the door Friday. It should've been:

Set oHeaven = GetObject("WinNT://MyDomain/MyGroup,Group")
MsgBox oHeaven.IsMember("WinNT://MyDomain/MyUser")

But one thing you may want to consider, is that the IsMember function does not recursively check membership. Thus if your GroupA contains only one member, GroupB, and your UserA is a member of GroupB, you may/may not get the results you desire.

Depending on how you want this reported, you may want to implement a recursive function:

Function IsMember(domain, group, user)

Dim oGroup
Dim oMemb
Dim aTemp

IsMember = False
Set oGroup = GetObject("WinNT://" & domain & "/" & group & ",group")
If oGroup.IsMember("WinNT://" & domain & "/" & user) Then
IsMember = True
Exit Function
End If
For Each oMemb In oGroup.Members
If oMemb.Class = "Group" Then
aTemp = Split(oMemb.ADsPath, "/")
IsMember = IsMember(aTemp(2), aTemp(3), user)
If IsMember Then Exit For
End If
Next
End Function Jon Hawkins
 
Hmm, thanks jonscott8. I will definitely look into that. Although the lack of recursion isn't causing a problem now and will probably never cause a problem I will certainly take a look at that code and make the change if works well.

Thanks,
-bitwise
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top