Powershell: Backup Lync 2013 configuration

One of the most important things to do when using Lync 2013 is to backup the Lync 2013 topology and configuration.
With these backup files and your ssl certificates you can restore your basic Lync environment (without the data).

With this powershell script you can easily backup these configuration files and as a bonus you can also backup the persistent chat database.

You have to run the script on a machine with the Lync cmdlets (Administrative Tools).

#
# Lync 2013 Backup script
# (c)2014 Hendricus Kramer - PepperByte
#
# Backup folder
$BKPLOC = "c:\lync"
# Lync 2013 pool
$LYNCPOOLFQDN = "lyncpool.peppercrew.nl"
# Database server for Persistent Chat, leave empty to skip
$PCHATDBS = "databaseserver.peppercrew.nl"
# $PCHATDBS = ""
#
############ DO NOT CROSS THIS LINE ;-) ############
#
$VERSION = "0.1"
$AUTHOR = "Hendricus Kramer - PepperByte"
$LOGGING = "$BKPLOC\lynclog-$FILEDATE.txt"
$FILEDATE = $(get-date -f dd-MM-yyyy)
$FILETIME = $(get-date -f HH:mm)
#
Clear
# Controleren of backuplocatie bestaat en anders aanmaken
if (Test-Path $BKPLOC) {""}Else{mkdir $BKPLOC | Out-Null}
#
Import-module Lync
#
$errorcounter = 0
#
#
"Date: $FILEDATE" | Tee-Object $LOGGING -Append
"Time: $FILETIME" | Tee-Object $LOGGING -Append
"" | Tee-Object $LOGGING -Append
#
"[x] Starting Lync 2013 backup version $VERSION by $AUTHOR" | Tee-Object $LOGGING -Append
"[x] Checking if backup location exists" | Tee-Object $LOGGING -Append
#
"[x] Creating backup" | Tee-Object $LOGGING -Append
##
if (Test-Path $BKPLOC\CsConfiguration-$FILEDATE.zip)
{"   [!] WARNING: CsConfiguration-$FILEDATE.zip already exists" | Tee-Object $LOGGING -Append; $errorcounter++}
Else
{Export-CsConfiguration -FileName $BKPLOC\CsConfiguration-$FILEDATE.zip;"   [x] SUCCES: $BKPLOC\CsConfiguration-$FILEDATE.zip was created succesfully" | Tee-Object $LOGGING -Append}
##
if (Test-Path $BKPLOC\CsLisConfiguration-$FILEDATE.zip) {"   [!] WARNING: CsLisConfiguration-$FILEDATE.zip already exists" | Tee-Object $LOGGING -Append; $errorcounter++}
Else
{Export-CsLisConfiguration -FileName $BKPLOC\CsLisConfiguration-$FILEDATE.zip;"   [x] SUCCES: $BKPLOC\CsLisConfiguration-$FILEDATE.zip was created succesfully" | Tee-Object $LOGGING -Append}
##
if (Test-Path $BKPLOC\CsUserData-$FILEDATE.zip) {"   [!] WARNING: CsUserData-$FILEDATE.zip already exists" | Tee-Object $LOGGING -Append; $errorcounter++}
Else
{Export-CsUserData -PoolFqdn $LYNCPOOLFQDN -FileName $BKPLOC\CsUserData-$FILEDATE.zip;"   [x] SUCCES: $BKPLOC\CsUserData-$FILEDATE.zip was created succesfully" | Tee-Object $LOGGING -Append}
##
if ($PCHATDBS -eq ""){"   [-] WARNING: Skipped Persistent Chat backup - No Database server configured" | Tee-Object $LOGGING -Append}Else{
if (Test-Path $BKPLOC\CsPersistentChatData-$FILEDATE.zip) {"   [!] WARNING: CsPersistentChatData-$FILEDATE.zip already exists" | Tee-Object $LOGGING -Append; $errorcounter++}
Else
{Export-CsPersistentChatData -DBInstance $PCHATDBS -Level All -FileName $BKPLOC\CsPersistentChatData-$FILEDATE.zip | Out-Null;"   [x] SUCCES: $BKPLOC\CsPersistentChatData-$FILEDATE.zip was created succesfully" | Tee-Object $LOGGING -Append}}
##
if ($errorcounter -gt 0) {"[x] Backup completed with warnings" | Tee-Object $LOGGING -Append}Else{"[x] Backup completed succesfully" | Tee-Object $LOGGING -Append}
#
"" | Tee-Object $LOGGING -Append