Last active
February 4, 2019 22:11
-
-
Save kiwi-cam/4583896cf7e01ecfba1f72ca8d5cbc64 to your computer and use it in GitHub Desktop.
Uses information from NZ Government to identify if a date is a New Zealand public holiday.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Uses information from NZ Government to identify if a date is a New Zealand public holiday. | |
.DESCRIPTION | |
This script pulls the lastest iCal from employment.govt.nz to confirm if a date is a public holiday. | |
If a public holiday is found, it's name is returned. | |
There is an assumption that the holidays supplied are single day events. | |
.PARAMETER Date | |
The date to be checked. | |
Default: The current date | |
.PARAMETER Region | |
If supplied, only the local regional holiday will be considered. | |
Valid values: | |
.EXAMPLE | |
./Get-Holiday.ps1 | |
.EXAMPLE | |
./Get-Holiday.ps1 -Date "12/25/2018" | |
.EXAMPLE | |
./Get-Holiday.ps1 -Date "01/21/2019" -Region Wellington | |
.NOTES | |
Version: 1.2 | |
Author: Cameron McConnochie | |
Creation Date: 5 Feb 2019 | |
Purpose/Change: Improved the ics file parsing | |
Version: 1.1 | |
Author: Cameron McConnochie | |
Creation Date: 4 Feb 2019 | |
Purpose/Change: Added Region selection to exclude other regions | |
Version: 1.0 | |
Author: Cameron McConnochie | |
Creation Date: 4 Feb 2019 | |
Purpose/Change: Initial script development | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$False)] | |
[DateTime]$Date = (Get-Date), | |
[Parameter(Mandatory=$False)] | |
[ValidateSet("Chatham Islands","Westland","Canterbury","Marlborough","Hawke's Bay","Canterbury (South)","Southland","Otago","Taranaki","Nelson","Auckland","Wellington")] | |
[String]$Region | |
) | |
#Initalise Variable | |
$HolidayName = $null | |
#Grab iCal file | |
$Content = Invoke-WebRequest "http://apps.employment.govt.nz/ical/public-holidays-all.ics" | |
#Error Checking | |
if(-not ($Content.Content.Substring(0,100) -like "BEGIN:VCALENDAR*")){ | |
throw "Error recieving iCal from employment.govt.nz" | |
} | |
#Split the iCal content into individual events | |
$Events = $Content.Content -split 'BEGIN:VEVENT' | |
#Process each event | |
ForEach ($Event in $Events) { | |
#Check if the "DTSTART" matches the date we're checking | |
If(($Event -split '\r?\n') -contains "DTSTART;VALUE=DATE:$(Get-Date -Date $Date -UFormat '%Y%m%d')") { | |
#Update the HolidayName variable with the "SUMMARY" if the dates match | |
$HolidayName = ((($Event -split '\r?\n') | Select-String -Pattern "SUMMARY") -split ':')[1] | |
} | |
} | |
#If nothing found on that date, return null | |
If(-not $HolidayName){ | |
return $null | |
} | |
If ($Region -and $HolidayName -like "*Anniversary*"){ | |
#If a region is supplied, and the Holiday found is a regional anniversary, only return the correct region | |
If ($HolidayName -like "*$($Region)*"){ | |
return $HolidayName | |
} else { | |
return $null | |
} | |
} else { | |
#No Region supplied or the holiday found is not a regional anniversary, return the holday name | |
return $HolidayName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment