[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 698
  • Last Modified:

Is User member of a given group? - ADSI

I use this code to determine if a user is a member of a given group:

http://www.freevbcode.com/ShowCode.asp?ID=797

It works on Win2k, but not on XP.  It blows on this line: Set oGroup = GetObject("WinNT://" & sDomain & "/" & GroupName)  throwing error -2147467259 (800401e4) with the description: "Automation error.  Unspecified error.

I know next to nothing about ADSI.  Tried Google but only found people asking the same question.

thanks.
0
lexicon1
Asked:
lexicon1
1 Solution
 
lexicon1Author Commented:
Here is the code the URL points to:

Public Function IsMember(GroupName As String, UserName As String) As Boolean

'PURPOSE: Determine if user specified by UserName is member
'of Group Specified by GroupName

'Returns: True if user is a member of group
'         False if user is not member of group,
'           User and/or group does not exist,
'           Or an error occurs

'requires: Win2000, ADSI, reference
'   to ActiveDS type library,

Dim oGroup As IADsGroup
Dim oSysInfo As New WinNTSystemInfo

Dim oRoot As IADs
Dim oDomain As IADsDomain
Dim sDomain As String

On Error GoTo ErrHandler

sDomain = oSysInfo.DomainName
Set oGroup = GetObject("WinNT://" & sDomain & "/" & GroupName)
IsMember = oGroup.IsMember("WinNT://" & sDomain & "/" & UserName)

ErrHandler:
Set oSysInfo = Nothing
Set oGroup = Nothing
Set oRoot = Nothing
Set oDomain = Nothing

End Function
0
 
lexicon1Author Commented:
Mikal613,

Could you be a little more specific?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
aaarrrgggghhhCommented:
Public Function IsMember(GroupName As String, UserName As String) As Boolean

'PURPOSE: Determine if user specified by UserName is member
'of Group Specified by GroupName

'Returns: True if user is a member of group
'         False if user is not member of group,
'           User and/or group does not exist,
'           Or an error occurs

'requires: Win2000, ADSI, reference
'   to ActiveDS type library,

Dim oGroup As IADsGroup
Dim oSysInfo As New WinNTSystemInfo
Dim oMember As IADs

Dim oRoot As IADs
Dim oDomain As IADsDomain
Dim sDomain As String

On Error GoTo ErrHandler

sDomain = oSysInfo.DomainName
Set oGroup = GetObject("WinNT://" & sDomain & "/" & GroupName)
For Each oMember In oGroup.Members
    If oMember.Name = UserName Then
        IsMember = True
        Exit For
    End If
Next

ErrHandler:
Set oSysInfo = Nothing
Set oGroup = Nothing
Set oRoot = Nothing
Set oDomain = Nothing

End Function
0
 
aaarrrgggghhhCommented:
The above seems to work on my PC running windows XP.
0
 
lexicon1Author Commented:
aaarrrgggghh,
Your solution blows on my XP box on the same line with the same error number as described in my initial post.
0
 
lexicon1Author Commented:
Dim oSysInfo As WinNTSystemInfo: Set oSysInfo = New WinNTSystemInfo
Dim sDomain As String: sDomain = oSysInfo.DomainName
Dim oGroup As IADsGroup


If oVersion.IsWinXP Then
Dim User As IADsUser: Set User = GetObject("WinNT://" & sDomain & "/" & p_sUserName & ",user")
For Each oGroup In User.Groups
    If p_sGroupName = oGroup.Name Then
      bReturn = True
      Exit For
    End If
Next
0
 
lexicon1Author Commented:
Opps, pressed the wrong key ...

This works on my XP box.  It uses a class that detemines what OS is running that I haven't included.
p_sGroupName and p_sUserName are string parameters.

Dim oSysInfo As WinNTSystemInfo: Set oSysInfo = New WinNTSystemInfo
Dim sDomain As String: sDomain = oSysInfo.DomainName
Dim oGroup As IADsGroup

If oVersion.IsWinXP Then
  Dim User As IADsUser: Set User = GetObject("WinNT://" & sDomain & "/" & p_sUserName & ",user")
  For Each oGroup In User.Groups
    If p_sGroupName = oGroup.Name Then
       bReturn = True
       Exit For
    End If  
  Next
Else
  ... logic from IsMember class posted above.
end if
0
 
GhostModCommented:
PAQd, 500 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now