Solved

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

Posted on 2013-10-22
21
1,303 Views
Last Modified: 2013-11-02
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
Comment
Question by:NCSA SCADA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 2
21 Comments
 
LVL 22

Expert Comment

by:mcsween
ID: 39591629
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
 

Author Comment

by:NCSA SCADA
ID: 39591668
correction I do get an error
Argument 'Prompt' cannot be converted to type 'string'
0
 

Author Comment

by:NCSA SCADA
ID: 39591678
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
Technology Partners: 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!

 

Author Comment

by:NCSA SCADA
ID: 39591684
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
 

Author Comment

by:NCSA SCADA
ID: 39591714
add some additional checks and the error is generated here

Dim obGroup As DirectoryEntry = obDirEntry.Children.Find("Administrators", "group")
0
 

Author Comment

by:NCSA SCADA
ID: 39591821
my bad again...... that error is from my testing
0
 

Author Comment

by:NCSA SCADA
ID: 39591835
so here is the real error
Exception has been thrown by the target of an invocation
0
 

Author Comment

by:NCSA SCADA
ID: 39591895
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
 

Author Comment

by:NCSA SCADA
ID: 39591917
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
 

Author Comment

by:NCSA SCADA
ID: 39592159
any help would be great
thanks experts
0
 

Author Comment

by:NCSA SCADA
ID: 39592170
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
 

Author Comment

by:NCSA SCADA
ID: 39592349
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39592692
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
 

Author Comment

by:NCSA SCADA
ID: 39592767
that is one of the ones I tried and get the same error
0
 

Author Comment

by:NCSA SCADA
ID: 39594465
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39594547
Did you get the same "Member does not exist" error when you tried the method from codeproject sample?
0
 

Author Comment

by:NCSA SCADA
ID: 39594584
Yes
0
 

Author Comment

by:NCSA SCADA
ID: 39594835
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
 

Author Comment

by:NCSA SCADA
ID: 39598441
any thoughts experts
0
 

Accepted Solution

by:
NCSA SCADA earned 0 total points
ID: 39606490
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
 

Author Closing Comment

by:NCSA SCADA
ID: 39618553
I did not find an answer here.  A friend solved the issue
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question