Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1401
  • Last Modified:

Remote server - Add local user to local group - VB.NET

I support over 100 remote servers that are not part of a domain.  I need the ability to create users and add them to groups from a central location (asp.net)

I have created a class for user management.
The create user function is working perfectly, but I am unable to get he add to group function to work.

I am also not returning anything from the catch in the add to group function

Any help would be great thanks

all information required by the functions are being passed by properties
*******
Create User Function
Public Function AddUser()
        Dim obDirEntry As DirectoryEntry = Nothing
        Try
            obDirEntry = New DirectoryEntry("WinNT://" & pServer)
            Dim entries As DirectoryEntries = obDirEntry.Children
            Dim obUser As DirectoryEntry = entries.Add(pUserName, "User")
            obUser.Properties("FullName").Add(pFullName)
            obUser.Properties("Description").Add(pDescription)
            Dim obRet As Object = obUser.Invoke("SetPassword", pPassword)
            obUser.CommitChanges()
            obDirEntry.Close()
            Return True
        Catch ex As Exception
            'Trace.Warn(ex.Message)
            Return ex.Message
        End Try
    End Function

Open in new window


***********
Add to group fuction
Public Function AddToGroup()
        Try

            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})
            obGroup.CommitChanges()
            obDirEntry.Close()
            Return True

        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

Open in new window

0
NCSA SCADA
Asked:
NCSA SCADA
  • 18
  • 2
1 Solution
 
mcsweenSr. Network AdministratorCommented:
I know this isn't how you are trying to do it but I find pstools to be much easier for this.

Install pstools http://technet.microsoft.com/en-us/sysinternals/bb896649.aspx

user the following to get a remote command prompt on a server
psexec \\computername -u username -p password cmd /k

Open in new window

I also have this nice vbscript to make things easier
Option Explicit

Dim wshell, strHost, strUID, strPWD

Set wshell = CreateObject("WScript.Shell")

strHost = "\\" & InputBox("Enter the Remote Computer name")
strUID = InputBox("Enter username")
strPWD = InputBox("Enter Password or * to be prompted")

If strHost <> "\\" Then 
	WScript.echo "psexec " & strHost & " -u " & strUID & " -p " & strPWD & " cmd /k"
Else
	wscript.echo "no host"
	WScript.Quit
End If

Open in new window


Then just use net user and net localgroup from the command line to do your user maintenance
Users - http://www.windows-commandline.com/add-user-from-command-line/
Groups - http://www.windows-commandline.com/add-user-to-group-from-command-line/
(To remove a user from a group use /delete switch instead of /add)
0
 
NCSA SCADAAuthor Commented:
correction I do get an error
Argument 'Prompt' cannot be converted to type 'string'
0
 
NCSA SCADAAuthor Commented:
im running all of this from a centralized web page
it is connecting to all of my servers and creating the user

I would rather not change they way I am performing the work... I think im close
just missing something
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
NCSA SCADAAuthor Commented:
sorry I did not see you entire post before commenting ...
I would still like to get this method to work - I plan to reuse this class for other things
0
 
NCSA SCADAAuthor Commented:
add some additional checks and the error is generated here

Dim obGroup As DirectoryEntry = obDirEntry.Children.Find("Administrators", "group")
0
 
NCSA SCADAAuthor Commented:
my bad again...... that error is from my testing
0
 
NCSA SCADAAuthor Commented:
so here is the real error
Exception has been thrown by the target of an invocation
0
 
NCSA SCADAAuthor Commented:
ok more info.... the inner exception is

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

I am creating the user... then calling the add to group function.. could the user creation not be complete?
0
 
NCSA SCADAAuthor Commented:
I tried separating the add to group behind a second button (just to test to see if it was a timing issue) but no love

any suggestions experts
0
 
NCSA SCADAAuthor Commented:
any help would be great
thanks experts
0
 
NCSA SCADAAuthor Commented:
more detal

Dim obUser As DirectoryEntry = obDirEntry.Children.Find(pUserName, "user")

this results in "WinNT://workgroup/10.106.3.220/test88"

it makes it past
 obGroup = obDirEntry.Children.Find("Administrators", "group")

added verification to check that

fails on adding the object to group

obGroup.Invoke("Add", New Object() {obUser.Path.ToString})

thanks
0
 
NCSA SCADAAuthor Commented:
I am at a loss here folks..... I have tried everything  I can think of and many different versions of other solutions searching the net....

I could really use a hand here

thanks
0
 
CodeCruiserCommented:
Great effort! Does following technique work(though its LDAP)?

http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#36
0
 
NCSA SCADAAuthor Commented:
that is one of the ones I tried and get the same error
0
 
NCSA SCADAAuthor Commented:
any help experts... I know this has to be doable...

I have functions for creating the user, creating groups, changing passwords
I just cant get the one for adding a user to a group to work...

another option.. does anyone have a VB.net function for adding a user to a group

Thanks for the help
0
 
CodeCruiserCommented:
Did you get the same "Member does not exist" error when you tried the method from codeproject sample?
0
 
NCSA SCADAAuthor Commented:
Yes
0
 
NCSA SCADAAuthor Commented:
Does this look like it could be the problem
This Code
Dim AD As New DirectoryEntry("WinNT://" & pServer & ",computer")
 MsgBox(AD.Path.ToString)

Open in new window


Returns this path
WinNT://10.106.3.220,computer

This Code
user = AD.Children.Find(pUserName, "user")
 MsgBox(user.Path.ToString)

Open in new window


returns this path
WinNT://workgroup/10.106.3.220/test88

the path for the user looks strange to me

any thoughts experts
0
 
NCSA SCADAAuthor Commented:
any thoughts experts
0
 
NCSA SCADAAuthor Commented:
I figured out a way to get this to work (thanks Jay from Frontier systems)

 Try
            Dim AD As DirectoryEntry = New DirectoryEntry("WinNT://" + pServer + ",computer")
            Dim grp As DirectoryEntry
            grp = AD.Children.Find(pGroup, "group")
            grp.Invoke("Add", New Object() {"WinNT://" & pUserName & ",user"})
            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox(ex.InnerException.Message)
            Return False
        End Try

Open in new window

0
 
NCSA SCADAAuthor Commented:
I did not find an answer here.  A friend solved the issue
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 18
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now