txgeekgirl1
Programmer
I have this bit of code and need to write to the Users folder in Active Directory. How would I add that Folder to the dirEntry var?
Code:
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
CreateAdAccount(UserName.Text, "", "", "", "")
End Sub
Public Sub CreateAdAccount(ByVal sUserName As String, _
ByVal sPassword As String, _
ByVal sFirstName As String, ByVal sLastName As String, _
ByVal sGroupName As String)
Dim catalog As Catalog = New Catalog()
Dim dirEntry As New DirectoryEntry()
' 1. Create user account
Dim adUsers As DirectoryEntries = dirEntry.Children
Dim newUser As DirectoryEntry = adUsers.Add("CN=" & sUserName, "User")
' 2. Set properties
SetProperty(newUser, "givenname", sFirstName)
SetProperty(newUser, "sn", sLastName)
SetProperty(newUser, "SAMAccountName", sUserName)
SetProperty(newUser, "userPrincipalName", sUserName)
newUser.CommitChanges()
' 3. Set the password
SetPassword(newUser, sPassword)
' 5. Add the user to the specified group
AddUserToGroup(dirEntry, newUser, sGroupName)
' 6. Enable the account
EnableAccount(newUser)
' 7. Close & clean-up
newUser.Close()
dirEntry.Close()
End Sub