Detecting Group Membership using VBScript - Techist - Tech Forum

Go Back   Techist - Tech Forum > Computer Software > Microsoft Windows and Software > Tips, Tricks & Tutorials
Click Here to Login
Closed Thread
 
Thread Tools Display Modes
 
Old 04-22-2006, 10:01 PM   #1 (permalink)
Techie Beyond Description
 
Osiris's Avatar
 
Join Date: Jan 2005
Location: Kentucky
Posts: 36,817
Send a message via ICQ to Osiris Send a message via AIM to Osiris Send a message via MSN to Osiris Send a message via Yahoo to Osiris
Default Detecting Group Membership using VBScript

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.
__________________
Osiris is offline  
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Copyright 2002- Social Knowledge, LLC All Rights Reserved.

All times are GMT -5. The time now is 02:34 PM.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2018, vBulletin Solutions, Inc.