I have the following example:
[pre]
$ErrorActionPreference = ‘Stop’
Function Get-Example {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True,
HelpMessage = 'Enter flavor'
)
]
[ValidateSet("Chocolate", "Strawberry", "Vanilla")]
[String[]]$Flavor
)
BEGIN {}
PROCESS {
Try {
Write-Output "Flavor selected: $Flavor"
}
Catch [System.Management.Automation.ParameterBindingException] {
$ErrorMessage = $_.Exception.Message
Write-Output "This failed" #"Error: [$ErrorMessage]"
}
}
END {}
} # End Get-Example
Get-Example -Flavor Mango
[/pre]
How do I capture the error if the user types a “wrong” value? I’ve been researching but the solutions I found didn’t work for me.
Any guidance will be appreciated.