The PowerShell function validates an instance of an input parameter using a regular expression _powershell

Source: Internet
Author: User

This article describes whether the arguments entered when you customize the PowerShell function can be constrained by regular expressions. Regular expressions to constrain input parameters, the validatepattern instruction is used.

We used Validateset as a smart cue for parameters, but it also had a check on the compliance of parameter inputs. Because Validateset is defined as a range of input parameters, not in this range of parameters, one is not intelligent hint, in addition, even if the input, it can not pass the function of the input parameters of the compliance check, so that the program can not continue execution.

So since Validateset is the form of a collection to check input parameters, then Validatepattern is a regular expression rule to the input parameters of the compliance check, then Hongo believe you can understand. Now let's take a look at how you can use regular expressions to check for compliance with input parameters:

Copy Code code as follows:

function Get-zipcode {
Param
[Validatepattern (' ^\d{6}$ ')]
[String]
$ZIP
)
"This is the ZIP code for you entered: $ZIP"
}

The above is a function that examines the input zip code, [Validatepattern (' ^\d{6}$ ')] that uses the ' ^\d{6}$ ' regular expression to check the compliance of input parameters. "^\d{6}$" This regular expression is a 6-digit number, which is the format of the ZIP code.

So, by validatepattern this instruction, as long as you understand regular expression, then you can carry on the powerful examination to the input parameter's compliance, thus greatly enhances the function the security.

About the PowerShell function using regular expressions to the input parameters of the compliance check, this article on the introduction of so many, I hope to help you, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.