1

I am writing a C# program to add a local user to a local group (Administrators, for example). I can create a new user, but I am not able to add it to a group. What I have found is if in this group is any domain user or group it is not working. When I try to add it to the group without domain users it is ok. I run this program as a local admin.

using (PrincipalContext pc = new PrincipalContext(ContextType.Machine))
{
    UserPrincipal user = new UserPrincipal(pc, Login, Password, true);
    user.Save();
    GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, "Administrators");
    group.Members.Add(user);
    group.Save();
}

When I run it as domain admin it works ok, but I need it only as local admin.

Any insight would be greatly appreciated.

PS: I found this: Adding Local User to Local Admin Group , it is great but it is also not working...

Update

I can search for this new user, it exists.

The exception I am getting is

System.Runtime.InteropServices.COMException. Network path not found

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
Jarda H
  • 11
  • 3
  • "*not working*" is not a technical description of a problem. Its like going to the doctor and saying "*I have a hurt*". Do you get any exceptions? Can you search for that user? :) – TheGeneral Dec 18 '20 at 08:49
  • Sorry, i am new here. :-) Yes, I can search for this new user, it exists. And exception is: System.Runtime.InteropServices.COMException. Network path not found. – Jarda H Dec 18 '20 at 08:54
  • Can it be somehow related to ? https://stackoverflow.com/questions/25060655/c-sharp-principalcontext-the-network-path-was-not-found – Tatranskymedved Dec 18 '20 at 10:44
  • I do not know, I think not. I do it on local machine only (win 10, machine is part of domain), technicaly I do not need domain but the machine is part of it. – Jarda H Dec 18 '20 at 10:55

0 Answers0