Johnny

Johnny

  • NA
  • 17
  • 0

Add User to Remote Group

Dec 16 2008 1:25 PM
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
 


Answers (8)