Hi guys,
I am attempting to create a small application to create users in one of several OU's in Active Directory. The issue is that it is not working correctly - no user is being created. I also need to know how to add in a users first name, last name and email address into the various fields in the Active Directory user record.
If anyone can help with this it would be much appreciated.
The code for this is below:
I am attempting to create a small application to create users in one of several OU's in Active Directory. The issue is that it is not working correctly - no user is being created. I also need to know how to add in a users first name, last name and email address into the various fields in the Active Directory user record.
If anyone can help with this it would be much appreciated.
The code for this is below:
Code:
Sub adeCreate()
Dim userName As String = frmMain.tStudent.Text
Dim strPath As String
If frmMain.cGraduate.Text = "GAE" Then
strPath = "Adl_GAE"
ElseIf frmMain.cGraduate.Text = "OU=Adl_GAE,OU=Students,OU=Users,OU=Adelaide,DC=GBS,DC=local" Then
strPath = "OU=Adl_Postgraduate,OU=Students,OU=Users,OU=Adelaide,DC=GBS,DC=local"
ElseIf frmMain.cGraduate.Text = "Undergraduate" Then
strPath = "OU=Adl_Undergraduate,OU=Students,OU=Users,OU=Adelaide,DC=GBS,DC=local"
Else
MsgBox("Error - Make Sure All Fields Have Been Populated", MsgBoxStyle.Critical, "Error")
End If
Dim oGUID As String = String.Empty
Dim connectionPrefix As String = "LDAP://" & strPath
Dim dirEntry As New DirectoryEntry(connectionPrefix)
Dim newUser As DirectoryEntry = dirEntry.Children.Add("CN=" & userName, "user")
newUser.Properties("samAccountName").Value = userName
newUser.CommitChanges()
oGUID = newUser.Guid.ToString()
newUser.Invoke("SetPassword", New Object() {"Welcome1"})
newUser.CommitChanges()
dirEntry.Close()
newUser.Close()
End Sub