By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

Need to add domain user to local group

P: n/a
I'm trying to add a domain user to a local group using the code below:

Dim LCL As New DirectoryEntry("WinNT://" + Environment.MachineName +
",computer")
Dim DOM As New DirectoryEntry("WinNT://us.ups.com")

Dim DOMUSR As DirectoryEntry = DOM.Children.Find("USERID", "user")

Dim LCLGRP As DirectoryEntry = LCL.Children.Find("LOCAL_GROUP", "group")

'LCLGRP.Invoke("Add", New Object() {DOMUSR.Path.ToString})
'LCLGRP.Children.Add(DOMUSR.Path.ToString, "user")
'LCLGRP.CommitChanges()

Trying the "Invoke" line throws "Exception Thrown by the target of an
Invocation."

I would sure appreciate some insight. Thanks!
Trying the "Children.Add" line throws "The Active Directory object
located at the path "WINNT://Domain/Computer/LOCAL_GROUP is not a
container."

*** Sent via Developersdex http://www.developersdex.com ***
Feb 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Have a look at this:

http://www.codeproject.com/dotnet/Wrapper_API.asp

--
Newbie Coder
(It's just a name)
Feb 13 '07 #2

P: n/a
Here's what I wrote a few years ago:

Imports System.DirectoryServices

Public Class frmDS
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents txtLoginName As System.Windows.Forms.TextBox
Friend WithEvents txtPassword As System.Windows.Forms.TextBox
Friend WithEvents txtConfirmPassword As System.Windows.Forms.TextBox
Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
Friend WithEvents txtSurname As System.Windows.Forms.TextBox
Friend WithEvents txtDescription As System.Windows.Forms.TextBox
Friend WithEvents btnAdd As System.Windows.Forms.Button
Friend WithEvents btnClear As System.Windows.Forms.Button
Friend WithEvents btnExit As System.Windows.Forms.Button
Friend WithEvents lblLoginNane As System.Windows.Forms.Label
Friend WithEvents lblPassword As System.Windows.Forms.Label
Friend WithEvents lblConfirm As System.Windows.Forms.Label
Friend WithEvents lblFirstName As System.Windows.Forms.Label
Friend WithEvents lblSurname As System.Windows.Forms.Label
Friend WithEvents lblDescription As System.Windows.Forms.Label
Friend WithEvents lblDomain As System.Windows.Forms.Label
Friend WithEvents txtDomain As System.Windows.Forms.TextBox
Friend WithEvents epLogin As System.Windows.Forms.ErrorProvider
Friend WithEvents lblHomeDirectory As System.Windows.Forms.Label
Friend WithEvents txtHomeDirectory As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()
Me.txtLoginName = New System.Windows.Forms.TextBox
Me.txtPassword = New System.Windows.Forms.TextBox
Me.txtConfirmPassword = New System.Windows.Forms.TextBox
Me.txtFirstName = New System.Windows.Forms.TextBox
Me.txtSurname = New System.Windows.Forms.TextBox
Me.txtDescription = New System.Windows.Forms.TextBox
Me.btnAdd = New System.Windows.Forms.Button
Me.btnClear = New System.Windows.Forms.Button
Me.btnExit = New System.Windows.Forms.Button
Me.lblLoginNane = New System.Windows.Forms.Label
Me.lblPassword = New System.Windows.Forms.Label
Me.lblConfirm = New System.Windows.Forms.Label
Me.lblFirstName = New System.Windows.Forms.Label
Me.lblSurname = New System.Windows.Forms.Label
Me.lblDescription = New System.Windows.Forms.Label
Me.lblDomain = New System.Windows.Forms.Label
Me.txtDomain = New System.Windows.Forms.TextBox
Me.epLogin = New System.Windows.Forms.ErrorProvider
Me.lblHomeDirectory = New System.Windows.Forms.Label
Me.txtHomeDirectory = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'txtLoginName
'
Me.txtLoginName.Location = New System.Drawing.Point(16, 24)
Me.txtLoginName.Name = "txtLoginName"
Me.txtLoginName.Size = New System.Drawing.Size(128, 20)
Me.txtLoginName.TabIndex = 0
Me.txtLoginName.Text = ""
'
'txtPassword
'
Me.txtPassword.Location = New System.Drawing.Point(160, 24)
Me.txtPassword.MaxLength = 32
Me.txtPassword.Name = "txtPassword"
Me.txtPassword.PasswordChar = Microsoft.VisualBasic.ChrW(35)
Me.txtPassword.Size = New System.Drawing.Size(128, 20)
Me.txtPassword.TabIndex = 1
Me.txtPassword.Text = ""
'
'txtConfirmPassword
'
Me.txtConfirmPassword.Location = New System.Drawing.Point(304, 24)
Me.txtConfirmPassword.MaxLength = 32
Me.txtConfirmPassword.Name = "txtConfirmPassword"
Me.txtConfirmPassword.PasswordChar = Microsoft.VisualBasic.ChrW(35)
Me.txtConfirmPassword.Size = New System.Drawing.Size(128, 20)
Me.txtConfirmPassword.TabIndex = 2
Me.txtConfirmPassword.Text = ""
'
'txtFirstName
'
Me.txtFirstName.Location = New System.Drawing.Point(16, 72)
Me.txtFirstName.Name = "txtFirstName"
Me.txtFirstName.Size = New System.Drawing.Size(128, 20)
Me.txtFirstName.TabIndex = 3
Me.txtFirstName.Text = ""
'
'txtSurname
'
Me.txtSurname.Location = New System.Drawing.Point(160, 72)
Me.txtSurname.Name = "txtSurname"
Me.txtSurname.Size = New System.Drawing.Size(128, 20)
Me.txtSurname.TabIndex = 4
Me.txtSurname.Text = ""
'
'txtDescription
'
Me.txtDescription.Location = New System.Drawing.Point(16, 120)
Me.txtDescription.Name = "txtDescription"
Me.txtDescription.Size = New System.Drawing.Size(272, 20)
Me.txtDescription.TabIndex = 5
Me.txtDescription.Text = ""
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(16, 200)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.TabIndex = 6
Me.btnAdd.Text = "Add"
'
'btnClear
'
Me.btnClear.Location = New System.Drawing.Point(96, 200)
Me.btnClear.Name = "btnClear"
Me.btnClear.TabIndex = 7
Me.btnClear.Text = "Clear"
'
'btnExit
'
Me.btnExit.Location = New System.Drawing.Point(360, 200)
Me.btnExit.Name = "btnExit"
Me.btnExit.TabIndex = 8
Me.btnExit.Text = "Exit"
'
'lblLoginNane
'
Me.lblLoginNane.Location = New System.Drawing.Point(16, 8)
Me.lblLoginNane.Name = "lblLoginNane"
Me.lblLoginNane.Size = New System.Drawing.Size(100, 16)
Me.lblLoginNane.TabIndex = 9
Me.lblLoginNane.Text = "Login Name:"
'
'lblPassword
'
Me.lblPassword.Location = New System.Drawing.Point(160, 8)
Me.lblPassword.Name = "lblPassword"
Me.lblPassword.Size = New System.Drawing.Size(100, 16)
Me.lblPassword.TabIndex = 10
Me.lblPassword.Text = "Password:"
'
'lblConfirm
'
Me.lblConfirm.Location = New System.Drawing.Point(304, 8)
Me.lblConfirm.Name = "lblConfirm"
Me.lblConfirm.Size = New System.Drawing.Size(100, 16)
Me.lblConfirm.TabIndex = 11
Me.lblConfirm.Text = "Confirm Password:"
'
'lblFirstName
'
Me.lblFirstName.Location = New System.Drawing.Point(16, 56)
Me.lblFirstName.Name = "lblFirstName"
Me.lblFirstName.Size = New System.Drawing.Size(100, 16)
Me.lblFirstName.TabIndex = 12
Me.lblFirstName.Text = "First Name:"
'
'lblSurname
'
Me.lblSurname.Location = New System.Drawing.Point(160, 56)
Me.lblSurname.Name = "lblSurname"
Me.lblSurname.Size = New System.Drawing.Size(100, 16)
Me.lblSurname.TabIndex = 13
Me.lblSurname.Text = "Last Name:"
'
'lblDescription
'
Me.lblDescription.Location = New System.Drawing.Point(16, 104)
Me.lblDescription.Name = "lblDescription"
Me.lblDescription.Size = New System.Drawing.Size(100, 16)
Me.lblDescription.TabIndex = 14
Me.lblDescription.Text = "Description:"
'
'lblDomain
'
Me.lblDomain.Location = New System.Drawing.Point(304, 56)
Me.lblDomain.Name = "lblDomain"
Me.lblDomain.Size = New System.Drawing.Size(100, 16)
Me.lblDomain.TabIndex = 15
Me.lblDomain.Text = "Domain:"
'
'txtDomain
'
Me.txtDomain.Location = New System.Drawing.Point(304, 72)
Me.txtDomain.MaxLength = 32
Me.txtDomain.Name = "txtDomain"
Me.txtDomain.Size = New System.Drawing.Size(128, 20)
Me.txtDomain.TabIndex = 16
Me.txtDomain.Text = ""
'
'epLogin
'
Me.epLogin.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink
Me.epLogin.ContainerControl = Me
'
'lblHomeDirectory
'
Me.lblHomeDirectory.Location = New System.Drawing.Point(16, 152)
Me.lblHomeDirectory.Name = "lblHomeDirectory"
Me.lblHomeDirectory.Size = New System.Drawing.Size(100, 16)
Me.lblHomeDirectory.TabIndex = 17
Me.lblHomeDirectory.Text = "Home Directory:"
'
'txtHomeDirectory
'
Me.txtHomeDirectory.Location = New System.Drawing.Point(16, 168)
Me.txtHomeDirectory.MaxLength = 260
Me.txtHomeDirectory.Name = "txtHomeDirectory"
Me.txtHomeDirectory.Size = New System.Drawing.Size(416, 20)
Me.txtHomeDirectory.TabIndex = 18
Me.txtHomeDirectory.Text = ""
'
'frmDS
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(450, 239)
Me.Controls.Add(Me.txtHomeDirectory)
Me.Controls.Add(Me.lblHomeDirectory)
Me.Controls.Add(Me.txtDomain)
Me.Controls.Add(Me.lblDomain)
Me.Controls.Add(Me.lblDescription)
Me.Controls.Add(Me.lblSurname)
Me.Controls.Add(Me.lblFirstName)
Me.Controls.Add(Me.lblConfirm)
Me.Controls.Add(Me.lblPassword)
Me.Controls.Add(Me.lblLoginNane)
Me.Controls.Add(Me.btnExit)
Me.Controls.Add(Me.btnClear)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.txtDescription)
Me.Controls.Add(Me.txtSurname)
Me.Controls.Add(Me.txtFirstName)
Me.Controls.Add(Me.txtConfirmPassword)
Me.Controls.Add(Me.txtPassword)
Me.Controls.Add(Me.txtLoginName)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.Name = "frmDS"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n
Me.Text = "Add User (Active Directory)"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim blnAdded As Boolean = False
If txtLoginName.Text.Trim <"" AndAlso txtPassword.Text.Trim <"" AndAlso
txtConfirmPassword.Text.Trim <"" AndAlso txtDomain.Text.Trim <"" Then
blnAdded = AddUser(txtLoginName.Text, txtPassword.Text,
txtConfirmPassword.Text)
ElseIf txtLoginName.Text.Trim <"" AndAlso txtPassword.Text.Trim <""
AndAlso txtConfirmPassword.Text.Trim <"" AndAlso txtDomain.Text.Trim <""
AndAlso txtFirstName.Text.Trim <"" AndAlso txtSurname.Text.Trim <"" Then
blnAdded = AddUser(txtLoginName.Text, txtPassword.Text,
txtConfirmPassword.Text)
ElseIf txtLoginName.Text.Trim <"" AndAlso txtPassword.Text.Trim <""
AndAlso txtConfirmPassword.Text.Trim <"" AndAlso txtDomain.Text.Trim <""
AndAlso txtFirstName.Text.Trim <"" AndAlso txtSurname.Text.Trim <""
AndAlso txtDescription.Text.Trim <"" AndAlso txtHomeDirectory.Text.Trim <>
"" Then
blnAdded = AddUser(txtLoginName.Text, txtPassword.Text,
txtConfirmPassword.Text)
Else
'MessageBox.Show("Please fill in the minimum required entries", _
' Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
SetError(epLogin, txtLoginName, "Please type in a login name")
End If
If blnAdded = True Then
MessageBox.Show(String.Format("User '{0}' added to '{1}' domain",
txtLoginName.Text, txtDomain.Text))
Else
MessageBox.Show(String.Format("Unable to add '{0}' to '{1}'",
txtLoginName.Text, txtDomain.Text))
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClear.Click
ClearAll()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Private Sub SetError(ByVal ep As ErrorProvider, ByVal txt As TextBox, ByVal
sError As String)
ep.SetError(txt, sError)
End Sub
Private Sub ClearAll()
SetError(epLogin, txtLoginName, "")
Dim c As Control
For Each c In Me.Controls
If TypeOf c Is TextBox Then
c.Text = ""
End If
Next
txtLoginName.Focus()
End Sub
Private Overloads Function AddUser(ByVal sUsername As String, ByVal
sPassword As String, ByVal sDomain As String) As Boolean
Try
Dim de As DirectoryEntry
de = New DirectoryEntry("WinNT://" & sDomain)
Dim entries As DirectoryEntries = de.Children
Dim user As DirectoryEntry = entries.Add(sUsername, "User")
Dim oUser As Object = user.Invoke("SetPassword", sPassword)
user.CommitChanges()
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Overloads Function AddUser(ByVal sUsername As String, ByVal
sPassword As String, ByVal sDomain As String, ByVal sFullName As String,
ByVal sDescription As String) As Boolean
Try
Dim de As DirectoryEntry
de = New DirectoryEntry("WinNT://" & sDomain)
Dim entries As DirectoryEntries = de.Children
Dim user As DirectoryEntry = entries.Add(sUsername, "User")
user.Properties("FullName").Add(sFullName)
user.Properties("Description").Add(sDescription)
Dim oUser As Object = user.Invoke("SetPassword", sPassword)
user.CommitChanges()
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Overloads Function AddUser(ByVal sUsername As String, ByVal
sPassword As String, ByVal sDomain As String, ByVal sFullName As String,
ByVal sDescription As String, ByVal sHomeDirectory As String) As Boolean
Try
Dim de As DirectoryEntry
de = New DirectoryEntry("WinNT://" & sDomain)
Dim entries As DirectoryEntries = de.Children
Dim user As DirectoryEntry = entries.Add(sUsername, "User")
user.Properties("FullName").Add(sFullName)
user.Properties("Description").Add(sDescription)
user.Properties("HomeDirDrive").Add(sHomeDirectory )
Dim oUser As Object = user.Invoke("SetPassword", sPassword)
user.CommitChanges()
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
--
Newbie Coder
(It's just a name)
Feb 13 '07 #3

P: n/a
Nevermind. My bad. I wasn't checking the InnerException. That revealed
that "the specified user is already assigned to the group".
It works fine (using the .Invoke line).

"Terry Olsen" <to******@hotmail.comwrote in message
news:Or**************@TK2MSFTNGP04.phx.gbl...
I'm trying to add a domain user to a local group using the code below:

Dim LCL As New DirectoryEntry("WinNT://" + Environment.MachineName +
",computer")
Dim DOM As New DirectoryEntry("WinNT://us.ups.com")

Dim DOMUSR As DirectoryEntry = DOM.Children.Find("USERID", "user")

Dim LCLGRP As DirectoryEntry = LCL.Children.Find("LOCAL_GROUP", "group")

'LCLGRP.Invoke("Add", New Object() {DOMUSR.Path.ToString})
'LCLGRP.Children.Add(DOMUSR.Path.ToString, "user")
'LCLGRP.CommitChanges()

Trying the "Invoke" line throws "Exception Thrown by the target of an
Invocation."

I would sure appreciate some insight. Thanks!
Trying the "Children.Add" line throws "The Active Directory object
located at the path "WINNT://Domain/Computer/LOCAL_GROUP is not a
container."

*** Sent via Developersdex http://www.developersdex.com ***

Feb 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.