Skip to content

Add User to Remote Group

Johnny

Asked Dec 16, 2008, 8:25 AM EST8 answers
Share
LinkedInX
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
 

8 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    I have a question, have you encountred an exception during the appliction process or simply it doesn't add a user?
    0
  • 0
    It adds the user fine. It throws the exception when trying to add the user to the group. I thought it might be a timing issue but I a test where I added the user waited then tried to add the user to the group but it still threw and exception.
    0
  • Bechir Bejaoui
    0
    Could you tel me what is the kind of the execption that is thrown or you can't determine it
    0
  • 0
    The specified local group does not exist. (Exception from HRESULT: 0x80070560)

    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.
    0
  • Bechir Bejaoui
    0
    Are you connecting to the right port? within the remote application
    0
  • 0
    I should not have to connect via any specific port to add a user to a group. I can create the user remotely by connecting to the machine using a admin account and password passed in by the directoryservices object. Or I specify the Enviroment,Computername instead of the remote computer it will work. But if I try to cycle through the groups on a remote computer it does not find any groups.
    0
  • 0
    Error (0x80004005): Unspecified error

    I had the some problem connecting to the remote windows with the error Error (0x80004005): Unspecified error. I resolved as follows:

    TypeScript
        //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;
            }
        }

    Hope that helps. 
    Helvio Junior
    www.helviojunior.com.br
    0