Click here to Skip to main content
15,944,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class ( project) for managing local user accounts.
I support more than 200 servers across north america that are not in a domain (long story).

My has no issue adding users, setting passwords and even creating groups, but I cant figure out how to add users to a group.
Here is the add user function - properties are used for gathering information - this works well
Public Function AddUser()
        Dim obDirEntry As DirectoryEntry = Nothing
            obDirEntry = New DirectoryEntry("WinNT://" & pServer)
            Dim entries As DirectoryEntries = obDirEntry.Children
            Dim obUser As DirectoryEntry = entries.Add(pUserName, "User")
            Dim obRet As Object = obUser.Invoke("SetPassword", pPassword)
            Return True
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

Here is my function for adding a user to a group - The msgboxes are there to gather error info

Public Function AddToGroup()
        Dim obDirEntry As New DirectoryEntry("WinNT://" & pServer & ",computer")
        Dim obUser As DirectoryEntry = obDirEntry.Children.Find(pUserName, "user")
        Dim obGroup As DirectoryEntry = obDirEntry.Children.Find("Administrators", "group")
        obGroup.Invoke("Add", New Object() {obUser.Path.ToString})
        Return True

    Catch ex As Exception
        Return False
    End Try
End Function

Here are the two errors

1) Exception has been thrown by the target of an invocation.

2) A memeber could not be added to or removed from the local group because the member does not exist

I have also added to other msgbox's to look at the paths returned and this is the results

obdirentry = WinNT://,computer

obUser = WinNT://workgroup/ ------
test88 is a valid user on the target system

Any help would be great... Thanks in advance

1 solution

Any thoughts on this one... If someone has a different way of doing this in I am open to that aswell
Share this answer
d_modugno 25-Oct-13 11:09am    
I could really use some help here.... someone must have done this before

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900