This article describes a $input variable when customizing the PowerShell function, which is a system-independent variable that represents the pipeline data entered in a block structure.
Let's take a look at how this $input variable is used in a function.
Copy Code code as follows:
function Test-pipeline {
$pipelineData = @ ($Input)
$Count = $pipelineData. Count
"Received $Count elements: $pipelineData"
}
The results of the operation are as follows:
Copy Code code as follows:
Ps> 1..10 | Test-pipeline
Received elements:1 2 3 4 5 6 7 8 9 10
In the example above, the $input variable is first converted to an array (that is, the variable $pipelinedata), the advantage of which is that the $pipelinedata variable can be used at any later. You may have a question, what is called arbitrary use of $pipelinedata variable, $input variable can not be used arbitrarily? Oh, that's right! $Input variable can only be used once! If you do not assign it to another variable, the second time you use the $input variable, you will doubt your eyes. Do not believe and see:
Copy Code code as follows:
function Test-pipeline {
$Count = $Input. Count
"Received $Count elements: $Input"
}
Ps> 1..10 | Test-pipeline
Received 1 1 1 1 1 1 1 1 1 1 elements:
This magical $input variable, in general, can receive pipe input as a block structure and can only be read once.
About the PowerShell function in the $input variable, this article on the introduction of so many, I hope to help you, thank you!