Edit: Adding clarification this my desire is only to show a progress bar with an known end (e.g. end of the pipeline) so that the function can provide a percentage toward completion (usually for large sets in the hundreds or thousands). I occasionally write functions to take objects from both pipeline or via a parameter so that the function can be flexible.
A Progress bar for an array of objects coming in from a parameter is simple enough, and is equivalent to pulling in the full pipeline set first, then processing them again. I have been avoiding the latter and I simply forego the write-progress in this scenario as it's not worth the impact.
I don't recall where I saw it, but someone mentioned $PSCmdlet.MyInvocation might provide the count, but perhaps I interpreted that incorrectly.
I've written some functions that take pipeline input and often would like to write a percentage progress bar for all the objects coming into the function via the pipeline.
Is there a way to get the total count at the beginning of the function?
I'm aware of how increase a counter as the function loops through the pipeline objects, but this only gets me the number of objects processed so far. I'd like to get a percentage of this by calculating it against the full pipeline count.
I've looked at the $MyInvocation and $PSCmdlet.MyInvocation properties, but the PipelineLength and PipelinePosition values area always '2' no matter how big the pipeline set is.
Note: this isn't what I'm focusing on as a solution, it's just one of the things I found that looked promising
Here's a test:
Function Test-Pipeline {
[CmdletBinding()]
PARAM(
[Parameter(ValueFromPipeLine=$true,ValueFromPipelineByPropertyName=$true)]
[Alias("FullName")]
[psobject[]]$Path
)
BEGIN{
$PSCmdlet.MyInvocation | Select *
}
PROCESS{
ForEach ($xpath in $Path) {
$filepath = Resolve-Path $xpath | Select -ExpandProperty Path
}
}
END{}
}
When I pipe in the contents of dir (which this folder contains 20 items), I get:
PS C:\Temp> Dir | Test-Pipeline
MyCommand : Test-Pipeline
BoundParameters : {}
UnboundArguments : {}
ScriptLineNumber : 1
OffsetInLine : 7
HistoryId : 213
ScriptName :
Line : Dir | Test-Pipeline
PositionMessage : At line:1 char:7
+ Dir | Test-Pipeline
+ ~~~~~~~~~~~~~
PSScriptRoot :
PSCommandPath :
InvocationName : Test-Pipeline
PipelineLength : 2
PipelinePosition : 2
ExpectingInput : True
CommandOrigin : Runspace
DisplayScriptPosition :