Validate PowerShell parameters
When you use parameters in your PowerShell script you might want to check the values for valid data. Of course you can do these checks with some if statements. But you can also specify valid values when defining the parameters.
|
|
In this example parameters $StringParameter1 and $IntegerParameter1 are checked with if statements (lines 18 to 24). The same rules are used to check the other parameters with ValidateSet (line 7) and ValidateRange (line 14). In both cases valid strings are “String1” and “String2” and valid integers are the numbers 0 to 5. Here you can see the error generated with an if statement
PS C:\> .\ValidatePowerShellParameter.ps1 -StringParameter1 "some other string" -StringParameter2 "String2" -IntegerParameter1 3 -IntegerParameter2 5
Error! Valid values for StringParameter1 are "String1"" and "String2".
At C:\ValidatePowerShellParameter.ps1:20 char:10
+ Throw <<<< "Error! Valid values for StringParameter1 are ""String1"" and ""String2""."
+ CategoryInfo : OperationStopped: (Fehler! Gültige... und "String2".:String) [], RuntimeException
+ FullyQualifiedErrorId : Error! Valid values for StringParameter1 are "String1"" and "String2".
and with ValidateRange.
PS C:\> .\ValidatePowerShellParameter.ps1 -StringParameter1 "String1" -StringParameter2 "String2" -IntegerParameter1 3 -IntegerParameter2 8
C:\ValidatePowerShellParameter.ps1 : Cannot validate argument on parameter 'IntegerParameter2'. The 8 argument is greater than the maximum allowed range of 5. Supply an argument that is less than 5 and then try the command again.
At line:1 char:120
+ .\ValidatePowerShellParameter.ps1 -StringParameter1 "String1" -StringParameter2 "String2" -IntegerParameter1 3 -IntegerParameter2 <<<< 8
+ CategoryInfo : InvalidData: (:) [ValidatePowerShellParameter.ps1], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,ValidatePowerShellParameter.ps1
There are more checks [available](https://technet.microsoft.com/en-us/library/dd347600.aspx ““about_Functions_Advanced_Parameters” in the Microsoft TechNet”):
- ValidateCount
- ValidateLength
- ValidatePattern
- ValidateRange
- ValidateScript
- ValidateSet
- ValidateNotNull
- ValidateNotNullOrEmpty
A short example with ValidateScript:
|
|
Unfortunately with ValidateScript it is not possible to access other parameters. So if valid values for a parameter depend on the value of another parameter you have to check the values outside the parameter defintion later in the code.
Also be careful when defining a default value for a parameter. If no other value for this parameter is given when executed there is no validation and you might want to check with your own code.
|
|
PS C:\> .\ValidatePowerShellParameter.ps1
IntegerParameter: 8
PS C:\> .\ValidatePowerShellParameter.ps1 -IntegerParameter 8
C:\ValidatePowerShellParameter.ps1 : Cannot validate argument on parameter 'IntegerParameter'. The 8 argument is greater than the maximum allowed range of 5. Supply an argument that is less than 5 and then try the command again.
At line:1 char:43
+ .\ValidatePowerShellParameter.ps1 -IntegerParameter <<<< 8
+ CategoryInfo : InvalidData: (:) [ValidatePowerShellParameter.ps1], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,ValidatePowerShellParameter.ps1