I found some code to do this out on the net the other day. I’ve modified it a little, and added the part that checks environment variables.
Note: 11th October 2010 – Since this post is so popular, I’ve cleaned up the code a bit and re-posted it below.
'Error Handling
On Error Resume Next
set objShell = WScript.CreateObject( "WScript.Shell" )
'Calls the isMember function with the specified group to see if the current user
' is a member of that group.
If isMember("GroupNameToCheckGoesHere") Then
'MsgBox("Is member") ' Do something here if they are a member of the group
Else
'MsgBox("Is not member") ' Do something here if they are not a member of the group
End If
' *****************************************************
'This function checks to see if the passed group name contains the current
' user as a member. Returns True or False
Function IsMember(groupName)
If IsEmpty(groupListD) then
Set groupListD = CreateObject("Scripting.Dictionary")
groupListD.CompareMode = TextCompare
ADSPath = EnvString("userdomain") & "/" & EnvString("username")
Set userPath = GetObject("WinNT://" & ADSPath & ",user")
For Each listGroup in userPath.Groups
groupListD.Add listGroup.Name, "-"
Next
End if
IsMember = CBool(groupListD.Exists(groupName))
End Function
' *****************************************************
' *****************************************************
'This function returns a particular environment variable's value.
' for example, if you use EnvString("username"), it would return
' the value of %username%.
Function EnvString(variable)
variable = "%" & variable & "%"
EnvString = objShell.ExpandEnvironmentStrings(variable)
End Function
' *****************************************************
' Clean up
Set objShell = Nothing
Excellent script, this is exactly what i was looking for. I appreciate your clean scripting style.
I will call this in a terminal services logon script to map a drive for only users in a specific group.
Thanks!
I am a bit of a VBScript noob. Here is my question. I am not seeing what the user variable is. Or can this script read all of the Active Directory??
It’s just grabbing the username out of an environment variable:
EnvString("username")Great script !!!
Thanks for sharing.
Thanks for the great utility, however as this is vbscript you should either define TextCompare in
groupListD.CompareMode = TextCompare
or write
groupListD.CompareMode = 1
otherwise it would do BinaryCompare (without option explicit turned on) or throw an error…
-roland
Thanks. I’m already using that comparemode
Hi Daniel,
Very nice script. How about if I want to find out if the all domain users are members of a certain group?
Thanks.
Mircea
I haven’t needed to do that in VBScript – but I used to use IFMember.exe in batch files. Apparently it doesn’t work anymore, but I haven’t used it for a long time.