Creating a PowerShell Weather Command Part 5
Parameter Validation
I’m continuing to make terrific progress on my function and Pester test. You’ve heard me write about this before, but you want to do everything you can to ensure your command will complete successfully before it even begins. One step you can take is to include parameter validation.
My function requires a time zone, and that string must follow a specific format. A regular expression isn’t enough because it can’t tell the difference between America/Los_Angeles
and Amerca/Los_Angeles
. I need a way to validate the parameter value as a legitimate time zone string.
Fortunately, I solved this problem a few years ago. I discovered another API that provides time zone information. I wrote a few functions that are part of the PSScriptTools module. I can use the Get-TZList function to get a list of all valid time zones.
Get-TZList -all | Where-Object {$_ -match "^\w+\/"} | Out-File .\tzlist.txt