Explore PowerShell (12) filter Filters

Source: Internet
Author: User

Filters are a special form of functions.

Define filter:

Filter (keyword) Filter Name (parameter) {code}

Unlike the definition function, the function uses the "$ input" variable to obtain information from the pipeline operator, while the filter uses the variable "$ _". In terms of data processing, functions and filters are also different. In a function, the "$ input" variable accepts all the information before performing operations. In a filter, the "$ _" variable accepts information from pipeline operators and performs filtering (operation) operations.
To sum up, for a function: the pipe operator transfers data to the function in the form of a set at a time, and the function must be processed cyclically or recursively. For filter: the code is executed once every time a data is imported into the MPs queue until all data is passed in.

At the end of the function described in the previous section, the following example is used:Copy codeThe Code is as follows: function fun {
Begin {
"Start"
$ I = 1
}
Process {
"Run" + $ I
$ _. Name
$ I ++
}
End {
"End"
}
}
Ls | fun

Here, change it:Copy codeThe Code is as follows: function fun {
"Run"
$ _. Name
}
Ls | fun

The running result is as follows:

As you can see, there is no "ls" output. In the original example, because the "process" declaration is used, the code block can be executed cyclically until the data processing ends. However, the results in this example are quite different.

If you change it to the following format:Copy codeThe Code is as follows: filter fun {
"Run"
$ _. Name
}
Ls | fun

Running result:

In this way, you can see the same output as the content in the previous section.
That is to say, functions containing "process" to mark code blocks also act as filters.

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.