Solved

vb.net and checking for local admin rights

Posted on 2014-12-10
4
80 Views
Last Modified: 2014-12-17
I have a button click which checks to see if a domain user is a member of the local admin group on a workstation.  It works when i supply a local workstation username, but when i supply a domain account, it tells me everytime theyre not apart of the admin group when theyre:

  Public Shared Function IsMemberOfGroup(userName As String, machineName As String, memberGroup As String) As Boolean
        Dim isMember As Boolean = False
        Using rootContext As New PrincipalContext(ContextType.Machine, machineName), _
              grp As GroupPrincipal = GroupPrincipal.FindByIdentity(rootContext, memberGroup), _
              usr As UserPrincipal = UserPrincipal.FindByIdentity(rootContext, IdentityType.SamAccountName, userName)
            If grp IsNot Nothing AndAlso usr IsNot Nothing Then
                ' Check if the user is a member of the group.
                isMember = grp.GetMembers(True).Contains(usr)
            Else
                isMember = False
            End If
        End Using
        Return isMember
    End Function

    Private Sub Button15_Click_1(sender As Object, e As EventArgs) Handles Button15.Click
        Dim str As String = tbUser.Text
        str = str.Remove(0, 5)
        Dim localusrname As String = str
        If IsMemberOfGroup(localusrname, TextBox1.Text, "administrators") Then
            tbadmin.Text = "Admin rights granted to " & tbUser.Text & " on Workstation: " & TextBox1.Text & " and Primary User has been updated in the local Registry."
        Else
            tbadmin.Text = "Admin rights have not been updated yet for " & tbUser.Text & " on Workstation: " & TextBox1.Text & "." & " Make sure you run the Fix Admin Rights tool first!" & " If you have, please verify it is successful and the user has access to the correct group, then reboot and re-check."
        End If
    End Sub

Open in new window

0
Comment
Question by:derek7467
[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 25

Expert Comment

by:chaau
ID: 40492667
For the group you need to use GroupPrincipal.FindByIdentity method:
Public Shared Function IsGroupMemberOfGroup(group As String, machineName As String, memberGroup As String) As Boolean
        Dim isMember As Boolean = False
        Using rootContext As New PrincipalContext(ContextType.Machine, machineName), _
              grp As GroupPrincipal = GroupPrincipal.FindByIdentity(rootContext, memberGroup), _
              grp1 As GroupPrincipal = GroupPrincipal.FindByIdentity(rootContext, IdentityType.SamAccountName, group)
            If grp IsNot Nothing AndAlso usr IsNot Nothing Then
                ' Check if the user is a member of the group.
                isMember = grp.GetMembers(True).Contains(grp1)
            Else
                isMember = False
            End If
        End Using
        Return isMember
    End Function

Open in new window

0
 

Author Comment

by:derek7467
ID: 40496027
Why is there a group and a membergroup?  Where do i define who the user is im searching for?
0
 

Accepted Solution

by:
derek7467 earned 0 total points
ID: 40496335
0
 

Author Closing Comment

by:derek7467
ID: 40504366
the info found on the link i provided resolved my issue
0

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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