Thursday, August 6, 2015

Parameter Validation

To summarize Glenn Sizemore from the Scripting Guys blog, here is how you can validate your PowerShell parameters when you write your own script (requires V2):
Param(
[ValidateSet("Department", "AppData")][string]$type,
[ValidateRange(10, 25)][int]$size,
[ValidateScript({Test-connection $_ -count 2 -quiet})][string]$computername
)

And since version 3, the PowerShell ISE is smart enough (via Intellisense) to show you your choices in a drop-down for a ValidateSet parameter. 

No comments:

Post a Comment