Skip to content

Instantly share code, notes, and snippets.

@petervandivier
Created May 26, 2023 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petervandivier/19a8ff90ed8b3961d072d49e4d6cd705 to your computer and use it in GitHub Desktop.
Save petervandivier/19a8ff90ed8b3961d072d49e4d6cd705 to your computer and use it in GitHub Desktop.
ValueFromPipeline processing in a function is serial unless you use a steppable pipeline
#Requires -PSEdition Core
# HT @santisq in powershell-slack
# https://powershell.slack.com/archives/C1RCWRDL4/p1685120926413089
function foo {
param(
[Parameter(Mandatory,ValueFromPipeline)]
[string]
$bar
)
begin {
$ScriptBlock = {"input is $_"; Start-Sleep 5}
$pipe = { ForEach-Object -Parallel $ScriptBlock }.GetSteppablePipeline()
$pipe.Begin($PSCmdlet)
}
process {
$pipe.Process($bar)
}
end {
$pipe.End()
}
}
$foo = @('a','b','c')
Measure-Command {$foo | foo} | select TotalSeconds
Measure-Command {foo $foo} | select TotalSeconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment