PowerShell Pipeline and brackets--powershell from zero start series VI

Source: Internet
Author: User

This issue mainly introduces the connection between multiple commands--pipeline

PowerShell connects commands to each other through pipelines, transferring the first command as input to the second command cmdlet, making it run in a joint
The use of pipe characters can greatly improve the efficiency, the original command needs to execute more than a single line to complete


Give me a chestnut.
Now you need to query all domain accounts that start with Zhangs and disable
Then we can run it like this

Get-aduser-filter {samaccountname-like "zhangs*"} | Set-aduser-enabled $False


Of course, the pipeline can be used more than one, assuming that your account exists in a text, you can write

Get-content C:\1.txt | Get-aduser-filter {Enabled-eq $True} | Set-aduser-enabled $False


But the pipeline is not the more the better, too many pipelines will reduce the efficiency of execution, here also please pay attention to


As you can see from the example above, you can query the ad user first and then set the ad user



But what happens if two unrelated data are executed?


Real-time proof, unable to execute successfully


So, how exactly does PowerShell transfer data to the pipeline??

In the following example, we make the first command command a, which produces some results. The second command becomes command B, which receives the result set generated by command A and then completes its own work

Get-content C:\Computers.txt | Get-service

When you run get-content, it puts the computer name in the text file into the pipeline. PowerShell then decides how to pass the data to the Get-service command. But PowerShell can only use a single parameter at a time to receive incoming data.

That is, PowerShell must decide which parameter of Get-service to receive the result of Get-content, which is called pipe parameter binding.

PowerShell has two ways to bind pipe parameters:

    • Byvalue

When pipe parameter binding is implemented using Byvalue, PowerShell confirms the type of data object generated by command A, and then looks at which parameter in command B can accept the type of object passed through the pipe.


You will see that the get-content command produces a result object that is a string. By querying the help, you can see that there is indeed a parameter-name in Get-service that can receive string type data from the Byvalue pipeline. You may have found a problem: this is not what we need-the content in our text file, the string object, refers to the computer name, not the service name, so it must not be executed.


    • Bypropertyname

The scheme also passes the output of command A to the parameters of command B, but Bypropertyname is slightly different from byvalue: multiple parameters of command B can be used simultaneously.
This feature is actually very simple to implement: Just look for the name of the property that matches the parameter name




However, if the command a output attribute and the parameter name of command B are not aligned, it becomes more difficult. Custom attributes are required to resolve


Sometimes, no matter how we try, we can't handle the output of the pipeline. Like Get-wmiobject.
Get-content. \computers.txt | Get-wmiobject–class Win32_BIOS
This parameter does not receive the computer name from the pipe, so how do we pass the data from other sources to the command?


You can then use parentheses to prioritize the commands in parentheses, and then pass the result as a parameter
Get-wmiobject–class win32_bios–computername (get-content. \computers.txt)



All right, the basic pipe and brackets are over, everybody, get the orders optimized, try to finish them in one line.


PowerShell Pipeline and brackets--powershell from zero start series VI

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.