About_Pipelines

Hello Team,

Appreciate if someone could clear my confusion around pipeline :heart:

Says "when multiple objects are sent across | being processed one at a time and sends to next cmdlet and so on ... "
I guess it is more or else a sequential process. How come sort-object works to whole collection of objects if it is getting one object at a time?
When cmdlet param accepts input pipeline byValue of type <PSObject> ( single object, no [] )
Is there a point to use ForEach-Object before using that cmd ? This again correlates to the above question, since pipeline process one-by-one .. So #3 from the below example is not necessary and skip it
Eg: Snippet of Test-NetConnection cmdlet

-ComputerName [<String>]
Specifies the Domain Name System (DNS) name or IP address of the target computer that runs the Dynamic Host Configuration Protocol
(DHCP) server service.

Required? false
Position? 1
Default value none
Accept pipeline input? True (ByValue, ByPropertyName)
Accept wildcard characters? false

#1:> ‘server1’,‘server2’,‘server3’ | Test-NetConnection
#2:> @(‘server1’,‘server2’,‘server3’) | Test-NetConnection
#3:> @(‘server1’,‘server2’,‘server3’) | ForEach-Object -Process {Test-NetConnection -Computername $_ }

There are certain cmdlets breaking with the rule of “one item at a time in the pipeline”. Otherwise they couldn’t work. That’s why they take more time to process usually.

Yes.

Why is the text cut off on the right side? In powershell 5 you can test the hosts in parallel like:

test-connection yahoo.com,microsoft.com -count 1 -asjob |receive-job -wait -auto

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
------        -----------     -----------      -----------                              -----    --------
DESKTOP-JQ... yahoo.com       98.138.219.231                                            32       65
DESKTOP-JQ... microsoft.com   40.76.4.15                                                32

Ack, I can’t even edit the last post. The edit is cut off.

It works for me on this thread but I had seen this in other threads already … you might post this in https://powershell.org/forums/forum/community-discussion-site-feedback/

@JS,

I’m just merely making a point, not to any specific cmdlet. I have seen coders using foreach-object in pipeline which doesn’t makes sense to me when cmdlet accepts input pipeline. Unless you have extra-doing in the process block I think you can ignore using foreach

 

You know that not all cmdlets support pipeline input, don’t you? And not all coders always write the best, most efficient and pure code following best practice rules. :wink:

It’s like Jeffrey Snover used to say: The world always has been, is and will always be - messy. :smiley: Sometimes the only thing what counts is - the code runs without errors and does what it’s suppost to do.

Yeah :grin:… well said :+1:

Eg 1:

PS# 'a','b','c' | Write-host
a
b
c
----

Eg 2: Saved Below content to .\Test-InputPipe.ps1

param (
  [Parameter(ValueFromPipeline = $true, Position = 0)]
  [object[]]$Name
)
Write-Host $Name
------------
PS# 'a','b','c' | .\Test-InPipe.ps1
c
What's the difference between Eg1 and Eg2 ? I am trying to understand the pipeline functionality which seems hard to crack apparently ☹
Only way it works 
'a','b','c' | foreach { .\Test-InPipe.ps1 $_ }
a
b
c

I think I’ve found answer. Included the write-host statement in process block of the script…

param (
  [Parameter(ValueFromPipeline = $true, Position = 0)]
  [object]$Name
)
process {
  Write-Host $Name
}