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
Loading
Helvio JuniorPosted Jul 11, 2014, 8:34 PM
Bechir BejaouiPosted Dec 20, 2008, 7:00 AM
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
JohnnyPosted Dec 18, 2008, 8:08 PM
Bechir BejaouiPosted Dec 18, 2008, 6:41 PM
JohnnyPosted Dec 17, 2008, 5:49 PM
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 BejaouiPosted Dec 17, 2008, 5:35 PM
JohnnyPosted Dec 16, 2008, 6:46 PM
Bechir BejaouiPosted Dec 16, 2008, 5:50 PM