VB Script

Nice script to check users group membership


Check if current user is a member of a certain group

'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


       '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, "-"


    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


WScript.Sleep 5000



MsgBox("Script took " & ElapsedTime & " Seconds to run")