Skip to content

Instantly share code, notes, and snippets.

@Packet-Lost
Last active January 18, 2020 18:29
Show Gist options
  • Save Packet-Lost/2cba3dbad9c29b37afbde62c75a29921 to your computer and use it in GitHub Desktop.
Save Packet-Lost/2cba3dbad9c29b37afbde62c75a29921 to your computer and use it in GitHub Desktop.
A function that displays errors during validated parameter type changes
function Show-ThisPsOddity {
param(
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][ValidateSet("foo", "bar", "baz", "match")][String]$UserInput,
[ValidateRange(1,2)][int]$Variant = 1
)
switch($variant) {
1 { if($UserInput -eq "match"){ $UserInput = @("foo", "bar", "baz") } }
2 { if($UserInput -eq "match"){ [array]$UserInput = "foo", "bar", "baz" } }
}
"Before the loop `$UserInput is $UserInput of type $(if($UserInput -is [array]){$UserInput.GetType().BaseType}else{$UserInput.GetType()})"
foreach($item in $UserInput) { "In the loop `$item is $item of type $(if($item -is [array]){$item.GetType().BaseType}else{$item.GetType()})" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment