Wednesday, January 14, 2015

Lync CMS Replication Script

There are any number of reasons why you would invoke replication of your CMS database in Lync. Say you've made a change and need to ensure that change is replicated out prior to testing. It's not hard to do with Lync, you can use the Invoke-CsManagementDatabase cmdlet, which works, but doesn't give any indication that replication is complete. To check replication status, you can use the Get-CsManagementStoreReplicationStatus cmdlet, but it can be a real pain, especially if replication takes a few minutes or so. Also, I wanted to have a block of code I code use in a script to kick off replication and wait until that replication is complete prior to moving on in the script.

Below, you'll find my quick solution. The code looks like this:

Invoke-Expression 'Invoke-CsManagementStoreReplication'
    Write-Host "Invoking Replication, please wait until it's complete:" -fore Yellow
    do
    {
        $result = Get-CsManagementStoreReplicationStatus | Select ReplicaFqdn, UpToDate
        write-host -nonewline "." -Fore Yellow
        sleep 3
    } While ($result.UpToDate -ne "True")
    Write-Host
    Get-CsManagementStoreReplicationStatus | Select ReplicaFqdn, UpToDate
    Write-Host
    Write-Host "Replication has completed. Thank you for your patience." -Fore Green


Paste the above code into Notepad and save as ScriptName.ps1, and you can run it from any PS prompt. When you do, it will look like:


The script has invoked replication and is in a do-while loop, checking to see if replication is complete every 3 seconds. It will write a period every iteration to give the user an indication that is still running:


Once it detects that replication is completed, it will print out all the CMS replicas and their status:


It's just easier for me than typing in the commands and hitting up arrow and enter every few seconds to see if replication is complete. You can paste the code directly into a script if you wish, or wrap it as a function that you can call several times within your script:

Function Replicate-CMS{
Invoke-Expression 'Invoke-CsManagementStoreReplication'
    Write-Host "Invoking Replication, please wait until it's complete:" -fore Yellow
    do
    {
        $result = Get-CsManagementStoreReplicationStatus | Select ReplicaFqdn, UpToDate
        write-host -nonewline "." -Fore Yellow
        sleep 3
    } While ($result.UpToDate -ne "True")
    Write-Host
    Get-CsManagementStoreReplicationStatus | Select ReplicaFqdn, UpToDate
    Write-Host
    Write-Host "Replication has completed. Thank you for your patience." -Fore Green

 }

So, as you can see, this is a very simple script and makes it easy to invoke and track the status of replication.

I hope some of you out there will find this useful. If you do, please follow and comment. If you have any suggestions to improve this, I welcome the feedback.

No comments:

Post a Comment

Thanks for reading and commenting. I look forward to it.