WhenÂ working with Powershell and Active Directory dependent software like Microsoft Exchange, or Microsoft Lync andÂ others, you can sometimes get inconsistent result. If your running multiple commands after each other, which depends on the previous command it sometimes fails. There is a simple solution to solve this problem.
I’ve primary noticed this when scripting or running powershell from other tasks (for example web-portals or automatic provisioning software) where the external software creates a new context for each command. For example you create a user-object, mailbox or similar, next you run a command to alter the previous created or modified object, but the second command fails (with errorÂ or silent)Â since it cannot find the previous new object or changed data on object. This only occures when having multiple domain controllers and data resides in Active Directory, for example a mailbox-user or lync enabled user.
This occures because of Active Directory must syncronize new or changed objects before the next command is run. For example new-mailbox runs towards your first domain-controller, then you run a set-mailbox to add an email-alias, which runs towards your second domain controller. Sometimes the two commands runs to the same domain controller and everything is fine, but when running to different domain controllers, the second command cannot find the new mailbox since the domain controllers have not syncronized the changes.
The solution is simple. Most powershell cmdlets which relies on Active Directory have an option for choosing which domain controller to use.
For exchange cmdlets it’s -DomainController, for Active Directory cmdlets it’s -Server.
new-mailbox -DomainController domaincontroller01 mymailbox set-mailbox -DomainController domaincontroller01 -identity mymailbox
Of course you have to do some testing to be sure the selected domain controller is online and responding to make sure it doesn’t stop your scripts.