Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
May 11, 2023

Creating a PowerShell Weather Command Part 3

Adding command validation

I hope you’ve been finding this series helpful. I made an assumption that you know a little bit about Pester. I’m showing you examples of real-world tests, but you might want to review the documentation at https://pester.dev/docs/quick-start.

At the end of the previous article, I had a Pester test file that validated parameters and design decisions. I also wrote enough of the function to pass the test. Since then, I decided to add one more design test. I want the Latitude and Longitude parameters to be positional and everything else to be named.

    Param(
        [Parameter(
            Position = 0,
            Mandatory,
            ValueFromPipelineByPropertyName,
            HelpMessage = "Specify the location's latitude."
        )]
        [double]$Latitude,
        [Parameter(
            Position = 1,
            Mandatory,
            ValueFromPipelineByPropertyName,
            HelpMessage = "Specify the location's longitude."
        )]
        [double]$Longitude,
        [Parameter(
            Mandatory,
            ValueFromPipelineByPropertyName,
            HelpMessage = "Specify the location's time zone like America/Los_Angeles."
        )]
        [string]$TimeZone,
        [Parameter(HelpMessage = "Specify if you want to use metric or imperial measurement units.")]
        [ValidateSet("Metric","Imperial")]
        [string]$Units = "Imperial"
    )

I updated the test file with new test cases.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.