Using parameter validation attributes
Attributes falling under this category define the attributes that we can use to validate the value of a parameter/variable itself. The following is a list of the most commonly used parameters:
AllowNull
/AllowEmptyString
: This attribute allows a mandatory parameter to accept a NULL value or empty string. Check the following example. When this attribute is not set, the function does not allow us to give an empty string as an input to the$VCName
parameter, as it is a mandatory input. When we comment out theAllowEmptyString
parameter, it throws an error:Function Get-VC{ [cmdletbinding()] Param( [Parameter(Mandatory = $true)] # [AllowEmptyString()] [String]$VCName ) Write-Host "vCenter Name: $VCName" }
Notice that, when this attribute is set, the function allows us to give an empty string as the input to the
$VCName
parameter:ValidateCount
: This attribute specifies the minimum and maximum number of values that a parameter...