PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

How to add a user to a local admin group in C#

 
 
=?Utf-8?B?UGF1bCBEYWx5IChNQ1Ap?=
Guest
Posts: n/a
 
      10th Jul 2004
Short Version:
How can I determine if the currently logged in user is a local administrator? & How can I add/remove a user to a group in C#? I want to add the currently logged in user to the local administrators group and then remove him/her later on.

Long Version:
I’m trying to write a login script that will install/update a piece of software on user’s computers. The problem that I’m running into is that the users don’t have administrative rights to their computers, so if they were to log in with this in their login script, it would fail. I tried using the runas command line utility, but it will not allow me to send the password argument via script (the user would be prompted for an administrative password). I then wrote a C# console application that took a username, password, domain and filename and would execute a process as that user. It is pretty much just like runas, but allows you to give it the password as an argument. (For security reasons, I’ll probably hard code the credentials into the executable.) This allows me to use elevated permissions to install most applications… one of the exceptions being the application that I need to install on users computers. The issue with this application is that it executes sub processes that need to run in the administrative context, but don’t inherit the administrative permissions that I give the parent process. I don’t have control of those sub processes, so I can’t call them with the new runas utility. I then decided that I could temporarily make the user an administrator while the software was installing. I figured that I could use the runas tool that I just created to call another C# application that would add the current user to the local admin group. Then I would install the software I initially wanted to install. On completion, the script would call another program that removes the current user from the admin group. My problem is that I don’t know how to add a user to a group in C# or how to check if the user is a local admin.

Here’s pseudo-code for what I’m trying to do…

IsAdmin = Is current user a local admin?

If Not isAdmin Then
Execute the new runas tool (that has administrative credentials hard coded into it) with another executable (that adds the current user to the local admin group) as an argument.
End If

Install software

If Not isAdmin Then
Execute the new runas tool (that has administrative credentials hard coded into it) with another executable (that removes the current user to the local admin group) as an argument.
End If

Thanks in advance for your help!
 
Reply With Quote
 
 
 
 
Natty Gur
Guest
Posts: n/a
 
      11th Jul 2004
Hi,

you can use WMI :

Add reference to System.Management.dll.
using System.Management;

...

string myDomain = "ChangeTo_DomainName";
string theUser = "ChangeTo_UserName";
string query = "SELECT * FROM Win32_GroupUser " +
"WHERE PartComponent = \"Win32_UserAccount.Domain='" + myDomain +
"',Name='" + theUser + "'\"";
SelectQuery selectQuery = new SelectQuery(query);
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(selectQuery);

foreach( ManagementObject obj in searcher.Get())
{
Console.WriteLine(obj.GetPropertyValue("GroupComponent"));
}
Console.ReadLine();

HTH

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)52-8888377


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add user to admin local group with gpo Jac Windows XP General 2 19th Apr 2007 03:09 PM
Add domain user to local group via web application running under local admin account Chad Dressler Microsoft ASP .NET 0 30th Dec 2006 01:27 AM
Adding user to local admin group =?Utf-8?B?VHJ1bmcgUXVhY2g=?= Windows XP Security 1 13th Aug 2006 05:34 PM
how to add domain user to local admin group? Evan Windows XP Configuration 0 16th May 2006 03:56 PM
Smart user removing domain admin group from local admin group Jody Riding Microsoft Windows 2000 Active Directory 3 30th Jun 2004 10:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:41 AM.