Solved

Is User member of a given group? - ADSI

Posted on 2004-10-28
668 Views
Last Modified: 2008-01-09
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
Question by:lexicon1
    9 Comments
     

    Author Comment

    by:lexicon1
    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
     
    LVL 48

    Expert Comment

    by:Mikal613
    0
     

    Author Comment

    by:lexicon1
    Mikal613,

    Could you be a little more specific?
    0
     
    LVL 2

    Expert Comment

    by:aaarrrgggghhh
    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
     
    LVL 2

    Expert Comment

    by:aaarrrgggghhh
    The above seems to work on my PC running windows XP.
    0
     

    Author Comment

    by:lexicon1
    aaarrrgggghh,
    Your solution blows on my XP box on the same line with the same error number as described in my initial post.
    0
     

    Author Comment

    by:lexicon1
    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
     

    Author Comment

    by:lexicon1
    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
     
    LVL 1

    Accepted Solution

    by:
    PAQd, 500 points refunded.

    GhostMod
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    931 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now