I am attempting to automate name changes for our students but am incurring inconsistent results with the Exchange Management Shell script. We use AD, Exchange 2007, & [PS] $host.version.tostring() = 1.0.0.0
We have a vb.net program that completes several algorithms combined with LDAP lookups to determine where and what naming scheme the person being changed has (staff, student, combined). The program also verifies the new name is not used. After we have determined the correct person, the AD name is changed with the following VB.Net logic:
Dim aDAccountDirectoryEntry As DirectoryEntry = renameSearchResultCollection(0).GetDirectoryEntry
Using aDAccountDirectoryEntry
aDAccountDirectoryEntry.Rename("CN=" & commonName) 'Not sure what all this command changes
If middleInitial.Length > 0 Then 'This statement handles an empty middle initial value
aDAccountDirectoryEntry.Properties.Item("initials").Value = UCase(middleInitial)
End If
With aDAccountDirectoryEntry.Properties
.Item("givenname").Value = firstName
.Item("sn").Value = lastName
.Item("displayname").Value = displayName.Trim() 'If no middle initial is present this trim remove extra spaces
End With
aDAccountDirectoryEntry.CommitChanges()
End Using
After this is complete we update the person’s shared drive and then update the person’s exchange properties with the following logic:
pipeLine.Commands.AddScript("set-mailbox -identity " & upnBeingProcessed & " -alias " & studentCommonName)
pipeLine.Commands.AddScript("set-mailbox -identity " & upnBeingProcessed & " -primarysmtpaddress " & newStudentMail & " -emailaddresspolicyenabled 0")
pipeLine.Commands.AddScript("$mbx=get-mailbox -identity " & upnBeingProcessed)
pipeLine.Commands.AddScript("$newaddrs = $mbx.emailaddresses|?{$_.IsPrimaryAddress -and $_.AddressString -eq " & strQuote & newStudentMail & strQuote & "}")
pipeLine.Commands.AddScript("set-mailbox -identity " & upnBeingProcessed & " -EmailAddresses $newaddrs
pipeLine.Commands.AddScript("set-mailbox -identity " & upnBeingProcessed & " -emailaddresspolicyenabled 1")
pipeLine.Commands.Add("Out-String")
Dim commandResults As Collection(Of PSObject)
commandResults = pipeLine.Invoke()
If Not commandResults Is Nothing Then
For Each obj As PSObject In commandResults
adMsg = obj.ToString()
nclSW.WriteLine(adMsg) 'log file
Console.WriteLine(adMsg)
Next
End If
As mentioned the challenge that I have is the results of the EMS script is not consistent.
[PS] C:\>get-mailbox 12345678@college.mstc.tech | fl name, alias, primarysmtpaddress, emailaddresses, emailaddresspolicyenabled
Name : samac1890 << This field is changed when the AD name is changed; i.e., not completed by EMS script.
Alias : samab1890 **Exchange fields not changed**
PrimarySmtpAddress : samab1890@mstc.edu
EmailAddresses : {smtp:samac1890@mstc.edu, SMTP:samab1890@mstc.edu}
EmailAddressPolicyEnabled : True
[PS] C:\>get-mailbox 12345678@college.mstc.tech | fl name, alias, primarysmtpaddress, emailaddresses, emailaddresspolicyenabled
Name : samad1890
Alias : samad1890 **Exchange fields changed**
PrimarySmtpAddress : samad1890@mstc.edu
EmailAddresses : {SMTP:samad1890@mstc.edu}
EmailAddressPolicyEnabled : True
I think what is happening is that the change in AD invokes a name change in Exchange at the same time the Exchange fields are being updated via the EMS script. I am not sure exactly though as I never get any results back when reviewing the commandResults after the PipeLine.Invoke statement.
I could possibly run the AD update from PowerShell but the communication is not the best when passing back through the command shell. (I’m not sure it this is due to a problem or just by design. If I am doing something incorrect can someone point it out to me?) I’m also not sure all that is being changed with the DirectoryEntry.Rename statement.
Another option would be to complete the name change with two programs; one for AD and one for Exchange. This makes it harder to determine if something did not complete correctly though.
Given our 1.0 version of PowerShell can an AD name change be done via the Set-User cmdlet? If so, could you provide an example of a script that does this? Our UPN is not based upon the name thankfully,
Does anyone have any suggestions on how to approach this better? Thanks in advance for your assistance. Steve