Validation attributes

PowerShell offers a number of validation attributes to test the content of arguments passed to parameters. There are two general classes of validation attribute; the first validates the argument as a single object, which tests the value as a whole:

  • ValidateNotNull
  • ValidateNotNullOrEmpty
  • ValidateCount
  • ValidateDrive

The second validates enumerated arguments. These validation attributes can be applied to parameters that accept arrays. The validation step applies to each element in the array. The enumerated argument validation attributes are:

  • ValidateLength
  • ValidatePattern
  • ValidateRange
  • ValidateScript
  • ValidateSet

The validation attributes are documented in about_Functions_Advanced_Parameters with the exception of the newer ValidateDrive attribute, which was introduced with PowerShell 5. The constructor for a validation attribute can be explored to determine the arguments it supports. This may be discovered using the ::new static method in PowerShell 5 and newer, for example, ValidateDrive:

PS> [ValidateDrive]::new

OverloadDefinitions
-------------------
ValidateDrive new(Params string[] validRootDrives)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.21