Validating Parameter Input
PowerShell can validate the arguments passed to cmdlet parameters in several ways. PowerShell can validate the length, the range, and the pattern of the characters of the argument. It can validate the number of arguments available (the count). These validation rules are defined by validation attributes that are declared with the Parameter attribute on public properties of the cmdlet class.
To validate a parameter argument, the PowerShell runtime uses the information provided by the validation attributes to confirm the value of the parameter before the cmdlet is run. If the parameter input is not valid, the user receives an error message. Each validation parameter defines a validation rule that is enforced by PowerShell.
PowerShell enforces the validation rules based on the following attributes.
ValidateCount
Specifies the minimum and maximum number of arguments that a parameter can accept. For more information, see ValidateCount Attribute Declaration.
ValidateLength
Specifies the minimum and maximum number of characters in the parameter argument. For more information, see ValidateLength Attribute Declaration.
ValidatePattern
Specifies a regular expression that validates the parameter argument. For more information, see ValidatePattern Attribute Declaration.
ValidateRange
Specifies the minimum and maximum values of the parameter argument. For more information, see ValidateRange Attribute Declaration.
ValidateScript
Specifies the valid values for the parameter argument. For more information, see ValidateScript Attribute Declaration.
ValidateSet
Specifies the valid values for the parameter argument. For more information, see ValidateSet Attribute Declaration.
See Also
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈