What determines whether the Powershell pipeline will unroll a collection?

前端 未结 2 1507
独厮守ぢ
独厮守ぢ 2020-12-10 04:57
# array
C:\\> (1,2,3).count
3
C:\\> (1,2,3 | measure).count
3

# hashtable
C:\\> @{1=1; 2=2; 3=3}.count
3
C:\\> (@{1=1; 2=2; 3=3} | measure).count
1

# a         


        
2条回答
  •  暖寄归人
    2020-12-10 05:53

    It seem to have something to do with how Measure-Object works and how objects are passed along the pipeline.

    When you say

    1,2,3 | measure
    

    you get 3 Int32 objects passed onto the pipeline, measure object then counts each object it sees on the pipeline.

    When you "unroll it" using your function, you get a single array object passed onto the pipeline which measure object counts as 1, it makes no attempt to iterate through the objects in the array, as shown here:

    PS C:\> (measure -input 1,2,3).count
    1
    

    A possible work-around is to "re-roll" the array onto the pipeline using foreach:

    PS C:\> (UnrollMe 1,2,3 | %{$_} | measure).count
    3
    

提交回复
热议问题