Automatically Updating the Edge Public IP in the Topology (home lab)

Having a fully functional Skype for Business home lab is tough. One of the difficulties is labbing an Edge server due to the certificate and public IP requirements.

Most people end up skipping the Edge role altogether in their labs, but those who don’t skip it end up needing to pay for at least one static IP, doing something with IPv6, or just put up with having a dynamic IP and use DDNS to update their DNS entries.

But with Skype for Business, it’s not enough to change the DNS records — you also need to update the topology with the new public IP.

This post addresses a very specific home lab scenario:

  • Single consolidated Edge server with 1 IP behind NAT
  • You can’t/won’t get a static IP from your ISP
  • DDNS is being used to update external DNS records

An example would be a topology like this (with my public IP being 5.5.5.5)

edge.png

Now each time my ISP gives me a new IP, I have to remember to publish my topology with that new IP. So instead of doing that, I wrote a script to do it for me.

A couple of disclaimers before I get started:

  • I wrote it really fast and it is poorly written and could break things (who needs logging or any sort of validation??)
  • You should never manually edit the topology xml file in production (duh)
  • Yes, I realize this is silly and the time I spent writing the post is greater than the time it would take me to update the topology over the next 5 years with a new IP
  • I mostly just wrote it to see if I could manually modify the topology

Now that that’s out of the way, I’ll explain what the script does

  1. Get’s your public IP by making a web request to an API
  2. Downloads your latest topology
  3. Checks the topology file for the public IP, if it’s not there then it swaps in the current public IP
  4. Repackages the files into a .zip
  5. Imports the modified topology to the CMS
  6. Publishes the new topology

The idea is to then run this as a scheduled task every now and then. Running this script does require permissions to download the topology as well as publish it. It also requires PowerShell v5 (because of the Archive cmdlets) and Skype/Lync Admin Tools installed.

You can find it on GitHub here or just check it out below (WordPress doesn’t do great with Powershell source code)

if(!(Get-Module SkypeforBusiness) -and !(Get-Module SkypeforBusiness)) {

Write-Host "Install admin tools -- the Lync/SfB module is not installed"

}

else {

    #get public IP
    $desiredEdgeIP = [string]($ip = Invoke-RestMethod -Uri 'https://api.ipify.org?format=json').ip

    #The string we will search the file for
    $existingValue = '(ConfiguredIPAddress="(\d{1,3}\.?){4}")'

    #What we want the value of the string to be
    $desiredValue = "ConfiguredIPAddress=""$($desiredEdgeIP)"""

    if ($desiredEdgeIP -match '(\d{1,3}\.?){4}') {

        #get topology
        $fileName = 'topology.zip'
        Remove-Item $fileName -Recurse -Force -ErrorAction SilentlyContinue

        #Get topology file
        Export-CsConfiguration -FileName $fileName

        #Export it to new fodler called 'extracted'
        Expand-Archive $fileName -DestinationPath .\extracted

        #Read the xml file
        $topologyFile = Get-Content .\extracted\DocItemSet.xml

        #Check to see if the value in the topology matches the current public IP
        if (!$topologyFile.Contains($desiredValue)) {

            #Do a find and replace on the IP, replace the XML
            $topologyFile -replace $existingValue,$desiredValue | Set-Content -Path .\extracted\DocItemSet.xml -Force

            #Pack up the 2 topology files into a zip
            Compress-Archive -Path .\extracted\* -DestinationPath new.zip

            #Import it into the CMS
            Import-CsConfiguration -FileName new.zip

            #Enable the new topology
            Enable-CsTopology

        }
        else {

            Write-Host "The public IP in the topology matches your current public IP. Congrats."

        }

    }

    #Cleanup
    Remove-Item .\extracted,.\topology.zip,.\new.zip -Recurse -Force -ErrorAction SilentlyContinue
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s