Code Snippet
'CREATE THE STUDENT ENTRY IN ACTIVE DIRECTORY
'Create a new common name (CN) entry in the container
dsUser = cnEntry.Children.Add(
"CN="
& userName, "user"
)If
middleInitDisplay.Length > 0 Then
'This statement handles an empty middle initial value
'General Tab
dsUser.Properties.Item(
"initials"
).Value = UCase(Left(middleInitDisplay, 1))End
If
'Add user properties - organized and commented per Active Directory Users & Computers UI tool
With
dsUser.Properties'General Tab
.Item(
"givenname"
).Value = firstNameDisplay.Item(
"sn"
).Value = lastNameDisplay.Item(
"displayname"
).Value = displayName.Trim() 'If no middle initial is present this trim remove extra spaces
.Item(
"description"
).Value = "Student"
.Item(
"mail"
).Value = email'Address Tab - used only for processing items
.Item(
"xx"
).Value = "xx"
'KEYs removed for security purposes (hence "xx") .Item(
"xx"
).Value = xx 'Account Tab
.Item(
"userprincipalname"
).Value = email.Item(
"sAMAccountName"
).Value = userName.Item("accountexpires"
).Value = GetLargeInteger(expireDate64) 'idli
'Profile Tab
'.Item("profilepath").Value = "\\xx\xx$\" & userName 'Disable roaming profiles
.Item(
"homedrive"
).Value = "H:"
.Item(
"homedirectory"
).Value = "\\xx\xx$\"
& userNameEnd
With
'dsUser.Invoke("SetPassword", password) 'Early bound vs late binding below (will fail Option Strict)
With
dsUser.CommitChanges()
.NativeObject.setPassword(password)
.NativeObject.accountDisabled =
False
.NativeObject.pwdLastSet = mustChangePassword
.CommitChanges()
End
With
'Assign the student to the Student group
studentGroup.Add(dsUser.Path)
studentGroupEntry.CommitChanges()'Create a mailbox for the student
cmdEM.Parameters.Clear()
'Clear out entries from the last student
cmdEM.Parameters.Add(
"Database"
, mailDB)cmdEM.Parameters.Add(
"Identity"
, email) 'The email variable contains the User Principle Name (UPN)
cmdEM.Parameters.Add(
"Alias"
, userName) 'Specified in the format xx
cmdEM.Parameters.Add(
"DomainController"
, dnsHostFQDName) 'FQDN from rootDSE
cmdResults = ems.PipelineInvoke(cmdEM,
CObj
(iErrors))For
Each
cmdItem In
cmdResultsConsole.WriteLine(cmdItem.Members(
"Name"
).Value.ToString)Next
'Check for pipeline errors
If
Not
iErrors Is
Nothing
Then
For
Each
errorItem In
iErrorsConsole.WriteLine(
"Error encountered creating the mailbox: {0} for display name {1} / EmplID: {2}"
, errorItem.ToString, displayName, emplId)Dim
adMsg As
String
= ("Error encountered creating the mailbox: "
& errorItem.ToString & " for display name "
& displayName & " / EmplID: "
& emplId)EventLog.WriteEntry(
"ADStudentUpdate"
, adMsg, EventLogEntryType.Error)' ADLogInstance.WriteEntry("ADStudentUpdate", adMsg, EventLogEntryType.Warning)
ADLogInstance.Close()
Next
'Verify that a mailbox was created
ElseIf
cmdResults.Count < 1 Then
Console.WriteLine(
"Mailbox not created for display name {0} / EmplID: {1}"
, displayName, emplId)Dim
adMsg As
String
= ("Mailbox not created for display name "
& displayName & " / EmplID: "
& emplId)EventLog.WriteEntry(
"xx"
, adMsg, EventLogEntryType.Error)ADLogInstance.Close()'If the mailbox was created update the status code of the associated account in Active Directory
Else
studentsMailBoxesCreated = studentsMailBoxesCreated + 1
dsUser.Properties.Item(
"xx"
).Value = "xx"
'KEYs removed for security purposes dsUser.CommitChanges()
End
If