PowerShell Splatting with ValidateSet

499 Views Asked by At

I cannot seem to get splatting to work when a functions parameter uses a validation set.

The below example produces the following error:

WhichFruit : Cannot validate argument on parameter 'fruit'. The argument "System.Collections.Hashtable" does not belong to the set "Apple,Orange" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.

Function WhichFruit {
    param(
    [ValidateSet("Apple","Orange")]
    [string]$fruit
    )

    Write-Host $fruit
}

$params = @{ fruit = "Apple" }

WhichFruit $params
1

There are 1 best solutions below

0
On

You didn't use the splatting operator @

Function WhichFruit {
    param(
    [ValidateSet("Apple","Orange")]
    [string]$fruit
    )

    Write-Host $fruit
}

$params = @{ fruit = "Apple" }

WhichFruit @params