官术网_书友最值得收藏!

  • Mastering PowerCLI
  • Sajal Debnath
  • 373字
  • 2021-07-09 21:47:48

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 the AllowEmptyString parameter, it throws an error:
    Function Get-VC{
        [cmdletbinding()]
    
        Param(
        [Parameter(Mandatory = $true)]
    #    [AllowEmptyString()]
        [String]$VCName
        )
        Write-Host "vCenter Name: $VCName"
    }
    Using parameter validation attributes

    Notice that, when this attribute is set, the function allows us to give an empty string as the input to the $VCName parameter:

    Using parameter validation attributes
  • ValidateCount: This attribute specifies the minimum and maximum number of values that a parameter accepts. PowerShell will generate an error if the number of values provided along with the command is greater or fewer than the specified ones. For example, in the following code, the minimum number of values permitted with the $VMName variable is 2 and the maximum is 10:
    Param
         (
                [parameter(Mandatory=$true)]
                [ValidateCount(2,10)]
                [String[]]
                $VMName
              )
  • ValidateLength: Using this attribute, we can restrict the number of characters that can be provided as values to a parameter. PowerShell will generate an error if the length of a value specified for a parameter or a variable is outside the range. In the following example, the value of the $VMName parameter must be between 1 to 10 characters:
    Param
              (
                [parameter(Mandatory=$true)]
                [ValidateLength(1,10)]
                [String[]]
                $VMName
              )
  • ValidatePattern: This attribute restricts the parameter value according to the regular expression specified by the pattern mentioned by the attribute. PowerShell will generate an error if the value does not match the regular expression pattern. In the following example, the $VMName variable must have a value that starts with two characters followed by any one of the special characters @,#, and! and followed by two digits, of which the first digit can lie between 0 to 9 and the last digit must be between 0 to 5:
         Param(
        [parameter(Mandatory=$true)]
              [ValidatePattern("[A-Z][a-z][@,#,!][0-9][0-5]")]
              [String[]]
              $VMName
            )

Note

For more details on parameter attributes, check out https://technet.microsoft.com/en-us/library/hh847743.aspx.

主站蜘蛛池模板: 金塔县| 专栏| 翁牛特旗| 剑阁县| 沙洋县| 阿克苏市| 靖安县| 宜宾市| 郁南县| 阆中市| 乐至县| 江永县| 武平县| 孙吴县| 尼勒克县| 辽阳市| 临颍县| 富宁县| 蚌埠市| 泰州市| 南宫市| 石台县| 安徽省| 军事| 双流县| 通山县| 高碑店市| 宜都市| 乌审旗| 缙云县| 正蓝旗| 始兴县| 扬州市| 苗栗市| 鄂托克旗| 安西县| 图们市| 莎车县| 舞阳县| 岢岚县| 甘谷县|