Detecting Group Membership using VBScript

Status
Not open for further replies.

Osiris

Golden Master
Messages
36,817
Location
Kentucky
Scripting languages such as Kixtart are often used to write login scripts due to their flexibility and power as compared with simple DOS-mode command strings. In Windows 2000 and later versions, the power to natively process VBScript and JScript login scripts was incorporated with the bundling of Windows Scripting Host into the operating system. This allowed for much more complicated scripting tasks to be achieved using natively-supported languages.

Unfortunately, the implementation of VBScript and JScript contains a few glaring omissions, the most frustrating of which (in our experience) is the lack of a command to establish whether a certain user is a member of a certain group (the equivalent of the Kixtart InGroup function). Although the coding extract below is certainly not unique or complicated, we have found it to be especially useful when creating large login scripts that perform numerous tasks based on group membership.

To check group membership using VBScript:

The following subroutine provides group membership checking. The function itself returns true if the user is a member and false if not.

Private Function IsMember(groupName)
Set netObj = CreateObject("WScript.Network")
domain = netObj.UserDomain
user = netObj.UserName
flgIsMember = false
Set userObj = GetObject("WinNT://" & domain & "/" & user & ",user")
For Each grp In userObj.Groups
If grp.Name = groupName Then
flgIsMember = true
Exit For
End If
Next
IsMember = flgIsMember
Set userObj = nothing
Set netObj = nothing
End FunctionThe function can be called from within another section of code using the following syntax:

retCode = IsMember("groupname")

retCode will be true is the user is a member of group groupname.
 
Status
Not open for further replies.
Back
Top Bottom