Vbs: Check if a user is part of a group

The function below is used to check if a user is part of an AD group.

The function returns True or False.

' *****************************************************
'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 = 1
        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 = oShell.ExpandEnvironmentStrings(variable)
End Function
' *****************************************************

PS: pour fonctionner la fonction IsMember à besoin de la fonction EnvString(variable)

Use :

If isMember("GROUP_NAME") Then
  **Action**
End If

 



Related Posts


GPO: Run a script when the computer starts

Presentation In this article, we will see how to set up a script that runs at startup of the post using Group Policy (GPO). The advantage of using this type of script is that it is executed with the A

GPO : Loopback Processing

Table of ContentsContext and issuesSolution: loopback processingConclusion In this tutorial, I will try to present to you simply the functioning of Loopback Processing in GPOs. It quickly becomes a he

Rename a domain controller

Intro In this tutorial, we will see how to rename domain controller in the "rules of the art". The name change happens in several times, here is a summary: Add a secondary name to the DCPermutation of