HaloPSA: Add Bank Holidays

Happy New Year everyone! Sorry for no blog in a long time, I’ve been distracted working on Kelvin Tegelaar’s amazing CIPP project (https://github.com/KelvinTegelaar/CIPP) and working on a big rewrite of my Hudu M365 integration, which should be finished shortly. I thought I would just do a quick blog on a problem I solved over Christmas. We had some wonderful fun realising that we had forgotten to setup all our bank holidays inside Halo PSA so we had just a couple of SLA alerts on the first bank holiday!

To avoid that happening in the future I thought I would write a quick script that will populate bank holidays in Halo from the UK Government’s json feed of them. It is set for England and Wales in the example, but would be very easy to swap to Scotland or Northern-Ireland instead. This is an interactive script which will ask you to confirm adding them to each Workday you have setup in Halo PSA.

You will need your Client ID, Client Secret and Halo URL for this to work. You will also need to make sure you have the latest version of the Halo PSA module (1.5.0).

https://github.com/lwhitelock/HaloPSA-Automation/blob/main/HaloPSA-Set-BankHolidays.ps1

# Set the Halo connection details
$VaultName = "Your Azure Keyvault Name"
$HaloClientID = Get-AzKeyVaultSecret -VaultName $VaultName -Name "HaloClientID" -AsPlainText
$HaloClientSecret = Get-AzKeyVaultSecret -VaultName $VaultName -Name "HaloClientSecret" -AsPlainText
$HaloURL = Get-AzKeyVaultSecret -VaultName $VaultName -Name "HaloURL" -AsPlainText

if (Get-Module -ListAvailable -Name HaloAPI) {
    Import-Module HaloAPI 
} else {
    Install-Module HaloAPI -Force
    Import-Module HaloAPI
}

# Connect to Halo
Connect-HaloAPI -URL $HaloURL -ClientId $HaloClientID -ClientSecret $HaloClientSecret -Scopes "all"

#Get all UK Bank holidays
$UKBankHolidays = Invoke-RestMethod -method get -uri "https://www.gov.uk/bank-holidays.json"

# Get just the england / wales bank holidays
$EnglandBankHolidays = $UKBankHolidays.'england-and-wales'.events

# Object array should have title and date properties for the bank holidays
$BankHolidays = $EnglandBankHolidays | Where-object { (get-date($_.date)) -ge (Get-Date) }

$Workdays = Get-HaloWorkday

# Loop all Halo Workdays
foreach ($Day in $Workdays) {
    # Get the full object
    $Workday = Get-HaloWorkday -WorkdayID $Day.id -IncludeDetails

    # Confirm if the holidays should be added to the workday
    Write-Host "Would you like to add bank holidays to $($Workday.name)"
    $Answer = Read-Host "Enter Y or N"
    if ($Answer -eq "Y") {
        # Parse existing bank holidays from Hudu
        $ExistingDays = $Workday.holidays | foreach-object { get-date($_.date) -format 'yyyy-MM-dd' }

        # Create an array of existing holidays to add to
        [System.Collections.Generic.List[PSCustomObject]]$Holidays = $Workday.holidays

        # Loop through all bank holidays from gov.uk
        foreach ($BankHoliday in $BankHolidays) {
            # Check if it is in Hudu
            if ($ExistingDays -notcontains $BankHoliday.date) {
                Write-Host "Adding $($BankHoliday.title) - $($BankHoliday.date)"
                # Add the holiday to the array
                $Holidays.add([pscustomobject]@{
                        name = $BankHoliday.title
                        date = $BankHoliday.date
                    })
                
            }
        }
        # Create the update object to add the holidays
        $UpdateWorkday = @{
            id       = $Workday.id
            holidays = $Holidays
        }

        # Perform the update
        $Null = Set-HaloWorkday -Workday $UpdateWorkday
    }
}

You may also like...

Leave a Reply

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