PowerShell–your own Autobot for Exchange Server!
For many IT professionals, PowerShell has come to be the best thing since sliced bread. In this post, Shamsher Dhanoa, our Senior PFE from the US East region, walks us through one more example of how PowerShell is very useful in safely automating tedious tasks like altering tons of configuration files.
The Challenge
As we navigate the various settings in Exchange Server we may have to modify a .config file; Exchange has a lot of these files; for example in the \bin directory:
We could just edit the file in Notepad but if we had 60 Exchange servers the downside of this method would be:
- Tedious
- Good chance of fat fingering a few
- Tedious (again!)
- Your passing boss may promptly outsource your job.
Another option could be to modify one file then just copy that file to the other servers overwriting each. But if the local administrators in, lets say, Kennebunkport, Maine, had customized their config file, they might not appreciate the heavy handed approach.
Another example - during an Exchange Server migration project we may have to move thousands of mailboxes from staging databases to final locations. As the Mailbox Replication Service is throttled, this could take a very long time and severely tax your patience.
PowerShell to the rescue!
Imagine that we need to change the below values from the default values to different values:
Let us try to do this en masse in PowerShell. First, we need a list of Client Access Servers in a variable to iterate through:
$ExServers = Get-ClientAccessServer
Next, let us backup the file we’re about to modify - just because a healthy paranoia is essential to an Exchange administrator! (Apart from lots of coffee and sometimes donuts). Here is how we backup the file:
$ExServers | %{$_.Name;copy-item "\\$_\c$\Program Files\Microsoft\Exchange Server\V14\bin\MSExchangeMailboxReplication.exe.config" "\\$_\c$\Program Files\Microsoft\Exchange Server\V14\bin\MSExchangeMailboxReplication.exe.BAKconfig"}
We would also need to stop the service so it’s not trying to touch the file at the same time that we are editing it:
$ExServers | %{$_.Name;stop-service -inputobject $(get-service -computername $_ -Name MSExchangeMailboxReplication)}
Now we are ready to make the changes. Note that the second part is one logical line, but separated by the ` character (PowerShell escape character.) The changes are achieved using the –replace operator to find and replace strings.
$ExServers | %{$MRSFile = get-content \\$_\c$\Program Files\Microsoft\Exchange Server\V14\bin\MSExchangeMailboxReplication.exe.config;
# Note that the lines that follow are separated by the ` escape character
$MRSFile | %{$_ -replace 'MaxActiveMovesPerSourceMDB = "5"$','MaxActiveMovesPerSourceMDB = "50"' `
-replace 'MaxActiveMovesPerTargetMDB = "2"$','MaxActiveMovesPerTargetMDB = "50"' `
-replace 'MaxCleanupRetries = "5"$','MaxCleanupRetries = "0"' `
-replace 'MaxActiveMovesPerTargetServer = "5"$','MaxActiveMovesPerTargetServer = "50"'} `
| set-Content "\\$_\c$\Program Files\Microsoft\Exchange Server\V14\bin\MSExchangeMailboxReplication.exe.config"}
Next, let us start the service to have the change take effect:
$ExServers | %{$_.Name;start-service -inputobject $(get-service -computername $_ -Name MSExchangeMailboxReplication)}
Then we verify the service did indeed start:
$ExServers | %{$_.Name;get-service -computername $_ -Name MSExchangeMailboxReplication}
Here is an example of the script in operation:
And if your morning donut is rudely interrupted by an angry Exchange admin from Kennebunkport, Maine – you can always revert the changes! Hopefully you never have to do that.
$ExServers | %{$_.Name;copy-item "\\$_\c$\Program Files\Microsoft\Exchange Server\V14\bin\MSExchangeMailboxReplication.exe.BAKconfig" \\$_\c$\Program Files\Microsoft\Exchange Server\V14\bin\MSExchangeMailboxReplication.exe.config}
Epilogue
I trust this walk through has given you more ideas on how you can apply PowerShell in almost every aspect of your day job – it just takes a bit of knowledge and some imagination. Thank you for your attention and do send us your feedback and let us know if there is anything else we can help you with!
Original content by Shamsher Dhanoa; posted by MSPFE editor Arvind Shyamsundar