Solved

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

Posted on 2013-10-22
21
1,263 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
  • 18
  • 2
21 Comments
 
LVL 21

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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now