Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

user within group

Status
Not open for further replies.

GilMerc

IS-IT--Management
Nov 12, 2003
115
CA
Hi all,

I find a function to know if a user is in a certain group (not a local group). I prefer to not use WMI to do this.

Gilles.
 
You should be able to get the information from Active Directory. Download the ADSI Scriptomatic tool from MS.
 
In the original, I think I must support all windows version and I don't want to install a less tools as possible.

After verification, I can use WMI because win98 is unsupported and WMI tools is install by default on client pc.

Unfortunately, our client don't use at this time Active Directory then the function can't be use this.

Gilles.

 
do they use NT or Novell then?

if its NT then use the WinNT provider and bind to the Group and then iterate through it, you will find tonnes of examples if you seach for them in this forum, infact you might find a FAQ on it
 
Set User = GetObject("WinNT://domainX/" & strUser & ",user")
Set Group = GetObject("WinNT://domainX/" & strGroup &",group")

wscript.echo Group.IsMember(User.ADsPath)

or.

Set User = GetObject("WinNT://domainX/" & strUser & ",user")
For Each aGroup In User.Groups
If aGroup.Name = "mygroup" Then
Wscript.Echo "they are"
End If

Next


what have you got yourself? are you working on a script? do you want to post your work and share with the rest of us?
 
I work in NT network. This is the code inspired on code that's found on the web.


----------------------------------
Private Function IsMember(groupName)

Dim NetObj, UserObj, Grp
Dim Domain, user, flgIsMember

Set netObj = CreateObject("WScript.Network")
domain = netObj.UserDomain
user = netObj.UserName
flgIsMember = 0
Set userObj = GetObject("WinNT://" & domain & "/" & user & ",user")

For Each grp In userObj.Groups
If UCASE(grp.Name) = UCASE(groupName) Then
flgIsMember = 1
Exit For
End If
Next
IsMember = flgIsMember
Set userObj = nothing
Set netObj = nothing

End Function

----------------------------------
Gilles
 
youre welcome for the help!
no offense but im in crap mode today so here are my thoughts.

1. it only works in the context of the currently logged on user

2. everytime you want to query if a user is a member of a groups it is going to
+ create a wscript.network object
+ retrieve domainname
+ retrieve username
+ bind to a user object
+ run through a collection of groups

so, from point 1. i guess you are using this for a logon script? either way it still sucks.
lets say you want to check if the user is a member of 5 groups,,,please dont tell me you are going to call this function 5 times,,,

5 X create a wscript.network object
5 X retrieve domainname
5 X retrieve username
5 X bind to a user object
5 X run through a collection of groups

vbscript is very powerful, but also very dangerous and seriously slow when inspiration is involved ;-)
 
so, ok what is the answer.
loop through the .Groups collection once. when looping through add each group.name to a dictionary object. have the function return a dictionary object to your calling script.
therefore from then on you have it in memory and all you have to do is

dicGroups.Exists("groupnameofinterest")

no more binding to objects and users which will speed things up in the long run.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top