Parameter value validation
In addition to validating the names of parameters, using CmdletBinding()
allows us to provide several types of validation rules for values of parameters. Validation rules enable us to provide checks that are performed by the parameter binding engine that specify when values are appropriate or not for the function. These parameter checking attributes can be divided into two groups: requirement validation and value checking. Requirement checking indicates whether the parameter is required or if various types of empty values are allowed. These attributes include the following:
Mandatory
AllowNull
AllowEmptyString
AllowEmptyCollection
ValidateNotNull
ValidateNotNullOrEmpty
Value checking attributes restrict the values that are allowed for a parameter. Value checking attributes include the following:
ValidateCount
ValidateLength
ValidatePattern
ValidateRange
ValidateScript
ValidateSet
Parameter attribute usage is illustrated in the following script:
Function...