$input variable usages in PowerShell _powershell

Source: Internet
Author: User

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!

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.