I am writing a application where I need to add a user to the local admin group on a remote computer. I can get it to work fine on a local computer but It will not work on a remote computer. Here is the code.
DirectoryEntry AD = new DirectoryEntry("WinNT://" + computerAccount, adminAccont, adminPassword);
DirectoryEntry NewUser = new DirectoryEntry();
NewUser = AD.Children.Add(username, "user");
NewUser.Invoke("SetPassword", new object[] { password });
NewUser.Invoke("Put", new object[] { "Description", description });
NewUser.CommitChanges();
AD.RefreshCache();
DirectoryEntry grp = AD.Children.Find("Administrator", "group");
if (grp != null)
{
grp.Invoke("Add", new object[] { NewUser.Path.ToString() });
}
But if I use this to add to a local account it works fine.
DirectoryEntry AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");
NewUser.Invoke("SetPassword", new object[] { "#12345Abc" });
NewUser.Invoke("Put", new object[] { "Description", "Test User from .NET" });
NewUser.CommitChanges();
DirectoryEntry grp;
grp = AD.Children.Find("Guests", "group");
if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); }
Any Ideas?
Thanks
Add User to Remote Group
8 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Bechir BejaouiRank #144 · 15.9K pointsPosted Dec 16, 2008, 12:50 PM EST
Johnny10 pointsPosted Dec 16, 2008, 1:46 PM EST
Bechir BejaouiRank #144 · 15.9K pointsPosted Dec 17, 2008, 12:35 PM EST
Johnny10 pointsPosted Dec 17, 2008, 12:49 PM EST
This is the expcetion. The group does exist. I have tried adding to Administrators, Users, Power Users etc but on remote machines it doesn't work. It works fine on local.
Bechir BejaouiRank #144 · 15.9K pointsPosted Dec 18, 2008, 1:41 PM EST
Johnny10 pointsPosted Dec 18, 2008, 3:08 PM EST
Bechir BejaouiRank #144 · 15.9K pointsPosted Dec 20, 2008, 2:00 AM EST
Ok, let's try this code found in this link might it help, I'm sure that there is a missed detail in your case. This code is done to enumerate users within groups
http://stackoverflow.com/questions/21514/enumerate-windows-user-group-members-on-remote-system-using-c
Helvio Junior2 pointsPosted Jul 11, 2014, 4:34 PM EDT
//Define path //This path uses the full path of user authentication String path = string.Format("WinNT://{0}/{1},user", server_address, username); DirectoryEntry deBase = null; try { //Try to connect with secure connection deBase = new DirectoryEntry(path, username, _passwd, AuthenticationTypes.Secure); //Connection test //After test define the deBase with the parent of user (root container) object nativeObject = deBase.NativeObject; deBase = deBase.Parent; } catch (Exception ex) { //If an error occurred try without Secure Connection try { deBase = new DirectoryEntry(path, username, _passwd); //Connection test //After test define the deBase with the parent of user (root container) object nativeObject = deBase.NativeObject; deBase = deBase.Parent; nativeObject = deBase.NativeObject; } catch (Exception ex2) { //If an error occurred throw the error throw ex2; } }