Public/Support/AutoAttendant/Get-PublicHolidayList.ps1
# Module: TeamsFunctions # Function: Helper # Author: David Eberhardt # Updated: 15-JAN-2021 # Status: Live function Get-PublicHolidayList { <# .SYNOPSIS Returns a list of Public Holidays for a country for a given year .DESCRIPTION Queries the Nager.Date API for public Holidays and returns a list per country and year. .PARAMETER CountryCode Required. ISO3166-Alpha-2 Country Code .PARAMETER Year Optional. Year for which the Holidays are to be listed. One or more Years between 2000 and 3000 If not provided, the current year is taken. If the current month is December, the coming year is taken. .EXAMPLE Get-PublicHolidayList [-CountryCode] CA [-Year] 2022 Lists the Holidays for Canada in 2022. The Parameters are positional, so can be omitted .INPUTS System.String .OUTPUTS System.Object .NOTES The Nager.Date API currently supports a bit over 100 Countries. Please query with Get-PublicHolidayCountry Evaluated the following APIs: Nager.Date: Decent coverage (100+ Countries). Free & Used Coverage: https://date.nager.at/Home/RegionStatistic TimeAndDate: Great coverage. Requires license. Also a bit clunky. Not considering implementation. Calendarific: Great coverage. Requires license for commercial use. Currently not considering development Utilising the Calendarific API could be integrated if licensed and the API key is passed/registered locally. .COMPONENT SupportingFunction TeamsAutoAttendant .FUNCTIONALITY Queries available Holidays for a specific Country from the Nager.Date API .LINK https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/Get-PublicHolidayList.md .LINK https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/about_Supporting_Functions.md .LINK https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/about_TeamsAutoAttendant.md .LINK https://github.com/DEberhardt/TeamsFunctions/tree/main/docs/ #> [CmdletBinding()] #[Alias('')] [OutputType([PSCustomObject])] param ( [Parameter(Mandatory, Position = 0, HelpMessage = 'ISO 3166-alpha2 Country Code (2-digit CC)')] [Alias('CC')] [ValidateScript( { if ($_ -in $(&$global:TfAcSbNagerAPICountryCode)) { $True } else { throw [System.Management.Automation.ValidationMetadataException] "'Value must be a valid ISO3166-alpha2 Two-Letter CountryCode supported by Nager.API. Use Intellisense for options. Country '$_' is not valid or not yet supported." } })] [ArgumentCompleter({ &$global:TfAcSbNagerAPICountryCode })] [String]$CountryCode, [Parameter(Position = 1, ValueFromPipeline, HelpMessage = 'Year')] [Alias('Y')] [ValidateRange(2000, 3000)] [int[]]$Year ) begin { Show-FunctionStatus -Level Live Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)" # Setting Preference Variables according to Upstream settings if (-not $PSBoundParameters.ContainsKey('Verbose')) { $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference') } if (-not $PSBoundParameters.ContainsKey('Debug')) { $DebugPreference = $PSCmdlet.SessionState.PSVariable.GetValue('DebugPreference') } else { $DebugPreference = 'Continue' } if ( $PSBoundParameters.ContainsKey('InformationAction')) { $InformationPreference = $PSCmdlet.SessionState.PSVariable.GetValue('InformationAction') } else { $InformationPreference = 'Continue' } # Handling Year if (-not $PSBoundParameters.ContainsKey('Year')) { $Today = Get-Date $Year = $Today.Year $null = $Today.Datetime -match '\d\d (.*?) \d' If ($Today.Month -eq 12) { $Year++ } Write-Information "INFO: $($MyInvocation.MyCommand) - Parameter Year not provided, as it is $($matches[1]), using year: $Year" } } #begin process { Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)" foreach ($Y in $Year) { $url = "https://date.nager.at/api/v3/publicholidays/$Y/$CountryCode" [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $Holidays = Invoke-RestMethod -Method Get -UseBasicParsing -Uri $url Write-Output $Holidays } } #process end { Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)" } #end } #Get-PublicHolidayList |