Solved

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

Posted on 2013-10-22
21
1,242 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
Comment Utility
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
Comment Utility
correction I do get an error
Argument 'Prompt' cannot be converted to type 'string'
0
 

Author Comment

by:NCSA SCADA
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
my bad again...... that error is from my testing
0
 

Author Comment

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

Author Comment

by:NCSA SCADA
Comment Utility
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
Comment Utility
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
Comment Utility
any help would be great
thanks experts
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:NCSA SCADA
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
that is one of the ones I tried and get the same error
0
 

Author Comment

by:NCSA SCADA
Comment Utility
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
Comment Utility
Did you get the same "Member does not exist" error when you tried the method from codeproject sample?
0
 

Author Comment

by:NCSA SCADA
Comment Utility
Yes
0
 

Author Comment

by:NCSA SCADA
Comment Utility
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
Comment Utility
any thoughts experts
0
 

Accepted Solution

by:
NCSA SCADA earned 0 total points
Comment Utility
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
Comment Utility
I did not find an answer here.  A friend solved the issue
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

16 Experts available now in Live!

Get 1:1 Help Now