Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to know in code if a windows network user is member of a network group?

Posted on 2006-11-06
4
Medium Priority
?
231 Views
Last Modified: 2010-04-18
Dear experts,
I need to use Windows identification to assign user rights in my software.
Thanks in advance
0
Comment
Question by:icad01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
SamuraiCrow earned 2000 total points
ID: 17887550
If you are looking for conditional batch processing you can download the ifmember.exe here:
http://www.microsoft.com/downloads/details.aspx?FamilyID=07c2f6d7-815e-4fa0-9043-4e4635ccd417&displaylang=en

Here is a quick syntax example:

:Start

ifmember "application group"
if not errorlevel 1 goto End
<run setup.exe or whatever other command you need here>
goto End

:End
0
 
LVL 9

Expert Comment

by:SamuraiCrow
ID: 17887558
Another quick article on ifmember.exe:
http://www.microsoft.com/technet/archive/winntas/tips/winntmag/grlogon.mspx?mfr=true

Hope this helps
Crow
0
 

Author Comment

by:icad01
ID: 17897870
Dear SamuraiCrow,
Thanks for your quick and valuable answer, sorry for the delay.
Great utility!
Do you know how to run it from Delphi, VC++ or VB?
Thanks
0
 
LVL 9

Expert Comment

by:SamuraiCrow
ID: 17899647
Generally I call the vb file from the batch file after the errorlevel command.  There are examples I've seen that discuss the VBS method of groupmembership checking (see below) but I've never tested them.  Unfortunately I don't have any experience with Delphi and very little experience with C++.  Here is the vbs syntax for a script that checks group membership then maps a couple of drives:

Option Explicit ' Force explicit declarations
'
' Variables
'
Dim WSHNetwork
Dim FSO
Dim strUserName ' Current user
Dim strUserDomain ' Current User's domain name
Dim ObjGroupDict ' Dictionary of groups to which the user belongs

Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")
'
' Wait until the user is really logged in...
'
strUserName = ""
While strUserName = ""
WScript.Sleep 100 ' 1/10 th of a second
strUserName = WSHNetwork.UserName
Wend
strUserDomain = WSHNetwork.UserDomain

' Read the user's account "Member Of" tab info across the network
' once into a dictionary object.

Set ObjGroupDict = CreateMemberOfObject(strUserDomain, strUserName)
If MemberOf(ObjGroupDict, "Domain Admins") Then
wscript.echo "Is a member of Domain Admins."
'REM this line to Map Network Drives

'Map network Drives here, UNREM the below lines:
'WSHNetwork.MapNetworkDrive "O:", "\\server1\share"
'WSHNetwork.MapNetworkDrive "Q:", "\\server2\share"

Else
wscript.echo "Is NOT a member of Domain Admins"
End If

Function MemberOf(ObjDict, strKey)
' Given a Dictionary object containing groups to which the user
' is a member of and a group name, then returns True if the group
' is in the Dictionary else return False.
'
' Inputs:
' strDict - Input, Name of a Dictionary object
' strKey - Input, Value being searched for in
' the Dictionary object
' Sample Usage:
'
' If MemberOf(ObjGroupDict, "DOMAIN ADMINS") Then
' wscript.echo "Is a member of Domain Admins."
' End If
'
'
MemberOf = CBool(ObjGroupDict.Exists(strKey))

End Function


Function CreateMemberOfObject(strDomain, strUserName)
' Given a domain name and username, returns a Dictionary
' object of groups to which the user is a member of.
'
' Inputs:
'
' strDomain - Input, NT Domain name
' strUserName - Input, NT username
'
Dim objUser, objGroup

Set CreateMemberOfObject = CreateObject("Scripting.Dictionary")
CreateMemberOfObject.CompareMode = vbTextCompare
Set objUser = GetObject("WinNT://" _
& strDomain & "/" _
& strUserName & ",user")
For Each objGroup In objUser.Groups
CreateMemberOfObject.Add objGroup.Name, "-"
Next
Set objUser = Nothing

End Function


Just keep in mind that if this answer isn't exactly what you are looking for you can post a question to the moderators to have points refunded (I won't take it personally) and have the question reposted in the programming forum.  My untimate goal is to leave you with something that you can use the points are really just for the fun of competition.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Within the dynamic life of an IT administrator, we hold many information in our minds like user names, passwords, IDs, phone numbers, incomes, service tags, bills and the order from our wives to buy milk when coming back to home.…
This may not be a text book method to resolve VSS backup issues but it seemed to have worked on few of the Windows 2003 servers we had issues while performing a Volume Shadow Copy backup. If you have issues while performing a shadow copy backup usin…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question