.net code to create AD user account

R

richb330

I'm trying to set up a program that will create a user account within active
directory based on a number of parameters selected on a form.

The following code should, i believe, create a user account within the AD.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim objADAM As DirectoryEntry ' Binding object.

Dim objUser As DirectoryEntry ' User object.

Dim strDisplayName As String ' Display name of user.

Dim strPath As String ' Binding path.

Dim strUser As String ' User to create.

Dim strUserPrincipalName As String ' Principal name of user.

Dim strPassword As String ' Password

' Construct the binding string.

strPath = LDAP://OU=temp,DC=domainname,DC=com

' Set ADAM object.

strUser = "George Michael"

strDisplayName = "George Michael"

strUserPrincipalName = "gmich1"

strPassword = "summer"

objADAM = New DirectoryEntry(strPath)

objADAM.RefreshCache()


' Create User.

objUser = objADAM.Children.Add(strUser, "user")

objUser.Properties("displayName").Add(strDisplayName)

objUser.Properties("userPrincipalName").Add( _

strUserPrincipalName)

objUser.Invoke("SetPassword", strPassword)

objUser.CommitChanges()

MsgBox("done")

End Sub


But when I run it in debug mode the code gets to
objUser.Invoke("SetPassword", strPassword)

and stops with:-

'an unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an
invocation.'

has anybody got any ideas?
 
S

Siva M

Can you try setting the password after calling CommitChanges()?

Also, make sure the password you set conforms to password complexity rules
set at the domain level.

-Siva

I'm trying to set up a program that will create a user account within active
directory based on a number of parameters selected on a form.

The following code should, i believe, create a user account within the AD.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim objADAM As DirectoryEntry ' Binding object.

Dim objUser As DirectoryEntry ' User object.

Dim strDisplayName As String ' Display name of user.

Dim strPath As String ' Binding path.

Dim strUser As String ' User to create.

Dim strUserPrincipalName As String ' Principal name of user.

Dim strPassword As String ' Password

' Construct the binding string.

strPath = LDAP://OU=temp,DC=domainname,DC=com

' Set ADAM object.

strUser = "George Michael"

strDisplayName = "George Michael"

strUserPrincipalName = "gmich1"

strPassword = "summer"

objADAM = New DirectoryEntry(strPath)

objADAM.RefreshCache()


' Create User.

objUser = objADAM.Children.Add(strUser, "user")

objUser.Properties("displayName").Add(strDisplayName)

objUser.Properties("userPrincipalName").Add( _

strUserPrincipalName)

objUser.Invoke("SetPassword", strPassword)

objUser.CommitChanges()

MsgBox("done")

End Sub


But when I run it in debug mode the code gets to
objUser.Invoke("SetPassword", strPassword)

and stops with:-

'an unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an
invocation.'

has anybody got any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top