473,388 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Check to see if USER is in the local administrators group and subgroups

I want to get the follow vbScript to work in VB.NET

Set objRun = CreateObject("wscript.Shell")
strUserName = objRun.ExpandEnvironmentStrings("%USERNAME%")
Dim strComputer, member, strUsername, IsMember
Dim grp, obj, objRun

strComputer = "."
'WScript.Echo strUsername
Set grp = GetObject("WinNT://" & strComputer & "/Administrators,group")

For Each member In grp.Members
'WScript.Echo member.Name & " (" & member.Class & ")"
If LCase(member.Name) = strUsername Then
IsMember = member.Name
Else

If (member.Class = "Group") Then
For Each obj In member.Members
If LCase(obj.Name) = strUserName Then
IsMember = obj.Name
'WScript.Echo obj.Name & " (" & obj.Class & ")"
End If
Next
End If
End If
Next

WScript.Echo IsMember & " is a Local Administrator!"

I currently have this VB.NET code but I don't know how to enumerate the members of each group inside the local administrator's group.

Dim strUserName As String = Environment.GetEnvironmentVariable("UserName")
Dim localMachine As New DirectoryEntry("WinNT://" & Environment.MachineName)
Dim admGroup As DirectoryEntry = localMachine.Children.Find("administrators", "group")
Dim members As Object = admGroup.Invoke("members", Nothing)

For Each groupMember As Object In CType(members, IEnumerable)
Dim member As New DirectoryEntry(groupMember)
If member.Name = strUserName Then
tbIsAdmin.Text = "YES"
Else
tbIsAdmin.Text = "NO"
End If
Next

Any help or suggestions are appreciated.

Thanks
Marty
Oct 13 '08 #1
2 13371
Plater
7,872 Expert 4TB
That code worked for me.
What is it doing for you?

Could you also do this:
Expand|Select|Wrap|Line Numbers
  1. WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
  2. bool a = wp.IsInRole("Administrators");
  3.  
Oct 13 '08 #2
That code worked for me.
What is it doing for you?

Could you also do this:
Expand|Select|Wrap|Line Numbers
  1. WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
  2. bool a = wp.IsInRole("Administrators");
  3.  

The WindowsPrincipal and IsInRole("Administrators") works!

The problem I was having with the above script was that it it was not enumerating the Domain groups in the local administrator's group. Thus if the logged on user was a member of a Domain group residing in the Local Admin's group it would return that the user is not in administrator's group when in fact he is.

However, the code you provided is a much better method in my opinion. Thanks so much!!!

Marty
Oct 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jon Ley | last post by:
I am having a problem restricting write access to tables in my database. In my database I have a table called, for the sake of argument, 'TableX'. In my SQL Server Logins, I have set up a login...
2
by: Frederick | last post by:
Hi, I am using ADAM as my Data Store for my web application and although this seems to be fine when using windows authentication with my local account when I try to create a new user and use...
1
by: Thomas Xu | last post by:
Hi guys, I'm trying to install DB2 v8.1 personal edition on WinXP, but keep getting complaint from installation program about invalid authority to perform the installation. I have Administrator...
6
by: Evgeny Zoldin | last post by:
Hi ALL. I have the configuration: 1. WinXP PRO with MS IIS 5.0 and installed ASP.NET 2. ASP.NET application A configured to authenticate only users from local Users group. I would like to de...
9
by: Sameh Ahmed | last post by:
Hello there Is there a way through dotNet to check if a certain user is a member of a specific group? I use ADSI to get the memberships of the user then compare them to the group I want to check,...
5
by: Rocky | last post by:
Hi, I have a webform, with 2 textboxs and a submit button. In the text box1, i enter a username and in textbox2 I enter the computer name. Both the username and computer name is in active...
5
by: Rocky | last post by:
Hi, I have a webform, with 2 textboxs and a submit button. In the text box1, i enter a username and in textbox2 I enter the computer name. Both the username and computer name is in active...
5
by: TravisTurman | last post by:
I'm a C# newbie and have inherited a program that runs at login. Part of the program looks at a text file to see which version is installed and then installs the latest greatest version if needed....
5
by: Michael Howes | last post by:
I'm writing a utility to manage a machines *local* accounts in c# I am getting all the users in a specific Group just fine but when I want to get some of the information on each user from their...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.