Skype for Business – Automatically set RGS Holiday Sets with PowerShell and JSON

Every Lync/Skype admin has the same nightmare with Response Group Holiday sets, something that has to manually be programmed each year for Public Holidays in your country.  What if you could script populating Holiday Sets automatically using PowerShell and an API?

I live in England and most of my customers are UK based so I needed a service that would give me Public Holidays for the UK.   Our government website lists all Public Holidays here: https://www.gov.uk/bank-holidays.  They are also conveniently provided in JSON format here: https://www.gov.uk/bank-holidays.json

I did look for a global service, but the one I found wanted $$$ to provide future Public Holidays: https://holidayapi.com/.   This script is based on the GOV.UK website and UK bank holidays, but you could amend it to work with another JSON provider.  Please also test in a Lab before rolling out to your live environment.

We can get the Public Holidays with PowerShell using Invoke-RestMethod:

Invoke-RestMethod -Uri "https://www.gov.uk/bank-holidays.json"

In this example I only want “england-and-wales” events which can be done as follows:

$holidays = Invoke-RestMethod -Uri "https://www.gov.uk/bank-holidays.json" | Select-Object -expandproperty england-and-wales
$holidays.events

We can also disregard any Public Holidays that occurred in the past with an If statement:

$holidays = Invoke-RestMethod -Uri "https://www.gov.uk/bank-holidays.json" | Select-Object -expandproperty england-and-wales
ForEach ($hol in $holidays.events){
    If (([DateTime]$hol.date) -gt $(get-date -Format yyyy-MM-dd)){
    $hol
    }
}

Now you have the basics of getting Public Holidays with JSON and PowerShell, you can now wrap this up into a PowerShell script that runs the New-CsRgsHoliday and Set-CsRgsHolidaySet cmdlets.  This can be ran as a scheduled task, maybe once a month is appropriate?

Set PoolName and HolidaySetName.

$PoolName = "FEPOOL.domain.local"
$HolidaySetName = "England and Wales Bank Holidays"

$holidays = Invoke-RestMethod -Uri "https://www.gov.uk/bank-holidays.json" | Select-Object -expandproperty england-and-wales

# Get the Holiday Set
$y = Try {Get-CsRgsHolidaySet -Identity "service:ApplicationServer:$PoolName" -Name $HolidaySetName}catch{$null}

# Clear the current Holiday List if it exists (Stops duplicates)
If ($y.name -eq $HolidaySetName){
    $y.HolidayList.Clear()
    Set-CsRgsHolidaySet -Instance $y
}

ForEach ($hol in $holidays.events){

    $StartDate = [DateTime]$hol.date
    $EndDate = $([DateTime]$hol.date).AddDays(1)
    
    # Ignore any previous public holidays
    If (([DateTime]$hol.date) -gt $(get-date -Format yyyy-MM-dd)){

        $x = New-CsRgsHoliday -StartDate $StartDate -EndDate $EndDate -Name $hol.title

        # If the current Holiday set exists add day to Holiday List
        If ($y.name -eq $HolidaySetName){
            $y.HolidayList.Add($x)
            Set-CsRgsHolidaySet -Instance $y
        }Else{
        # Create the holiday set
            New-CsRgsHolidaySet -Parent "service:ApplicationServer:$PoolName" -Name $HolidaySetName -HolidayList($x)
        }
    }
}

Get-CsRgsHolidaySet | select -ExpandProperty HolidayList

Output from final Get-CsRgsHolidaySet command

 

England and Wales Bank Holidays now show in the Response Group Configuration Portal and should always be up to date.  Again, test in a lab first.

 

2 Replies to “Skype for Business – Automatically set RGS Holiday Sets with PowerShell and JSON”

  1. Pingback: Skype for Business – Automatically set RGS Holiday Sets with PowerShell and JSON | Chris Hayward.co.uk – JC's Blog-O-Gibberish

  2. Pingback: Skype for Business – Automatic RGS Holidayset | skype4bworld

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.