If you are not familiar with pipeline than you should first read this.
Pipeline is a cool concept (The parallel ,associative,chaining ... concept), but it can be cooler that these. In powershell the pipelines are object pipelines .
Beside byte stream-based pipelines, there are also object pipelines. In an object pipeline, the processes output objects instead of texts; therefore removing the string parsing tasks that are common in UNIX shell scripts. Windows PowerShell uses this scheme and transfers .NET objects. (WIKI)
Object pipelines and bounded parameters are 2 of the greatest things in powershell pipelines.
A | B | C
Actual mean this:
because B doesn’t really take the output of A. There’s some manipulation going on to prepare the output of A as the input of B. Strip these characters from these columns, replace this pattern with this other pattern, etc.
Pipeline is a cool concept (The parallel ,associative,chaining ... concept), but it can be cooler that these. In powershell the pipelines are object pipelines .
Beside byte stream-based pipelines, there are also object pipelines. In an object pipeline, the processes output objects instead of texts; therefore removing the string parsing tasks that are common in UNIX shell scripts. Windows PowerShell uses this scheme and transfers .NET objects. (WIKI)
Object pipelines and bounded parameters are 2 of the greatest things in powershell pipelines.
Object pipeline
In Unix tis pipleine chain:A | B | C
Actual mean this:
A | | B | | C
In powershell commands spit out objects. For example, if the output of a PowerShell command is a date, then the command returns a .NET object representing a date, not a text string.(Poweshell know to do some casting when it needed).
Lets see the same in Unix vs Powershell/
In powershell:
Get-Process | Where-Object { $_.WS -gt 500Kb } | Sort-Object -Descending ProcessNa
In Unix:
ps -F | awk '{ if($5 > 500) print }' | sort -r -k 64,70
As you can see in powershell we are workinh with Object, so we dont need to guess the textual place of workingset parameter in the ps output and we dont need to filter it by complicated awk and sort based on the output format from awk.In powershell we work on Object.
Bound Parameter
This will work:
Get-ChildItem . *.cs -r | foreach { get-content $_.fullname } | ...
But the coll thing that this will also work:
Get-ChildItem . *.cs -r | get-content | ...
How come that we dont need to run foreach on the list result of Get-ChildItem?
The reason is that powershell use a concept called bound parameters.
Powershell try to bound the output result to one of the " pipeline input bounded parameter" of get-content.
If there is a matching a bound is done.
If we take a look on get-content help than we can see that it has Path input parameter that define like this:
-Path
Accept Pipeline Input? true (ByPropertyName)
ByPropertyName means that powershell will try to bind based on matching name. Powershell can also bind by value.
If you want to learn more on how powershell do the bind read this.
In additional
There are three cmdlets that are frequently used on the pipeline:
- Select-Object – filters which attributes of the object will passed through the pipeline
- Where-Object – filters objects based on one or more criteria e.g. name
- Foreach-Object – loops through a script block for each object passing down the pipeline
אין תגובות:
הוסף רשומת תגובה