Skip to content

Instantly share code, notes, and snippets.

@devlead
Created May 13, 2015 09:17
Show Gist options
  • Save devlead/5f584eec40d17a479f96 to your computer and use it in GitHub Desktop.
Save devlead/5f584eec40d17a479f96 to your computer and use it in GitHub Desktop.
ValidateScript is very powerful to validate script / fucntion parameters, example below validate country based on rest service
Param(
[ValidateScript(
{
$countries = ((Invoke-RestMethod -Uri https://raw.githubusercontent.com/devlead/ISO-3166-Countries-with-Regional-Codes/master/all/all.json)| % Name)
if ( $countries -contains $_)
{
$true
}
else
{
throw "Valid countries:`r`n`t$($countries -join "`r`n`t")"
}
}
)]
[string]$Country
)
"You chosed an valid country : $Country"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment