Adding domain user account to local administrator group

Z

ZzZoel

Hi all,

I'm new to this group but I've found many usefull discussions here.

Here's my problem/question. I want to add a domain account to a local
administrator group of one of the domain servers via a webservice
written in C#. I've read most discussion on creating a user and adding
the user to a group using the directoryservices namespace but so far I
haven't been successfull.

Below some lines of code I've used and works but creates a new local
user:

//******************************************
DirectoryEntry _localContainer = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer", "administrator", "password");

DirectoryEntry _userNew = _localContainer.Children.Add("test
user","user") as DirectoryEntry;
_userNew.Invoke("SetPassword", new object[] {"test"});
_userNew.Invoke("Put", new object[]{"Description","test account"});
_userNew.CommitChanges();
//*******************************************

Instead I want to add an existing domain account to the administrator
group of the server2003 machine the webservice is located on.

Any ideas or hint would be very much appreciated!

Thanx,

Suly
 
Z

ZzZoel

Ok, I've found the solution, it's actually quite simple.

Code:

//Bind and get the local directory container of the administator group
DirectoryEntry grp = new DirectoryEntry("WinNT://" +
Environment.MachineName + "/Administrators,group");

try {
// Add user to administrators alias
grp.Invoke("Add","WinNT://[domain]/[username],user");
grp.CommitChanges();
}
catch(Exception cex)
{
Console.WriteLine(cex.Message + "\n" + cex.StackTrace + "\n" +
cex.Source);
Console.ReadLine();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top