DAY 8 - Advance Pipeline















At this, point you've learned to pretty effective with PowerShell's Pipeline. Running Pipeline commands is powerful, accomplishing in one line what used to take several lines of the script. But if I said you can even do better than this, in this blog we'll dig deep into the pipeline and discover its powerful capabilities. 

How PowerShell Passes Data Down to Pipeline

Whenever you string two commands in a pipeline, PowerShell has to figure out how to get the output of the first command to the input of the second command. Let say the first command as command A and the second command as command B. A will produce something and B, which needs to accept A's output as input and then do its own thing.

Command A | Command B

For example, suppose you have a text file that contains one computer name on each line. You might want to use those names as the input to some command, telling that command which computers you want to run against.

Get-content .\computers.txt | Get-Service

First Get-Content command will fetch all the computers names in the pipeline. PowerShell then has to decide how to get those to the Get-Service command. The trick with PowerShell is that commands can only accept input on a Parameter where Parameter can accept pipeline values. That means PowerShell has to figure out which parameter of Get-Service will accept the output from Get-Content. This figuring-out process is called Pipeline Parameter Binding. PowerShell binds the parameter value by 2 methods,

1. By Value
2. By PropertyName

Pipeline - Input ByValue

With this method, PowerShell looks at the type of object produced by command A and tries to see if any parameter of command B can accept that type of object from the pipeline. You can determine this for yourself: first, pipe the output of command A to Get-Member, to see what type of object Command A is producing. 








Then, examine the full help of Command B to see if any parameter accepts that type of data from the pipeline ByValue.






You will find that Get-content produces objects of the type system.string. You will also find that Get-service does have a parameter that accepts a string from pipeline ByValue.

The problem with our pipeline is, -Name parameter which accepts the pipeline input is for service names not for the computer name. If we run the below command, it takes whatever in the text file as an input and passes it to parameter -Name not to -Computername parameter.

Get-content .\computers.txt | Get-Service

PowerShell permits one parameter to accept a given type of objects from the pipeline ByValue. This means -Name parameter accept string input from the pipeline ByValue, no other parameter can do so.

PipeLine: Input ByPropertyName

With this approach, we will still look to attach the output of command A to the parameter of command B. With this method, it's possible for multiple parameters of command B to become involved. Once again, pipe the command A to Get-Member and look for the syntax of command B.










The output of command A has one property whose name corresponds to a parameter in command B. After that, we need to see if that parameter from command B accepts pipeline input ByPropertyName.








Now take a look at the example, how we can pipeline using ByPropertyName.

Create a simple comma separated CSV file on a notepad as given below in the picture.










Now pipeline Get-Member to the command Import-csv.









You can clearly see, columns from csv became properties, and each data row in the csv became an object. Now check the help for New-Alias to see if this property exist.









If you check the full help for New-Alias Cmdlet, you will find that both -Name and -Value parameter accepts pipeline input ByPropertyName. Now we can run below command to see if we able to pass values ByPropertyName in the pipeline.

import-csv A:\Blogs\test.csv | new-alias

The result will be a new Alias.

This is a powerful technique for passing data from one command to another, and for accomplishing the task in the minimum number of commands.

That sums up this blog, in next one we will discuss more in details about PowerShell Pipeline.

Comments

Popular posts from this blog

DAY-1 : POWERSHELL…WHAT IT IS>>>AND WHY

Day-5: PowerShell Modules & PSSnapin

DAY-7: POWERSHELL OBJECT'S