count pipeline objects within Begin{}Process{}End{} blocks

Welcome Forums General PowerShell Q&A count pipeline objects within Begin{}Process{}End{} blocks

This topic contains 5 replies, has 5 voices, and was last updated by

3 years, 8 months ago.

  • Author
  • #29731

    Topics: 3
    Replies: 2
    Points: 0
    Rank: Member


    I have a use case, that I must use Begin{}Process{}End{} blocks for a function.

    And I pipe the objects to this function.

    Now I need to count the pipeline objects in the function, i'm told that, I can iterate all the objects in Process{} block and count the number, and do my real taks in End{} block. This methode works, but not beautiful, this method go throug every object at first.

    Im also told that not use Begin{}Process{}End{} blocks, that I will have $Input.count, but I must use Begin{}Process{}End{} blocks 🙂

    Can anyone help me please ?


  • #29732

    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    You can't know in advance. Objects are piped in one at a time, so you can't know in the BEGIN block how many there will be.

    What you can do is simply increment a variable in the PROCESS block, and save each incoming object into an array. Then put the rest of your code into the END block. You'll know how many objects you have then, and you can do whatever you need. This will, however, block the pipeline – like Sort-Object does – and will consume more memory, as you're caching objects rather than dealing with them one at a time.

  • #29734

    Topics: 0
    Replies: 2
    Points: 0
    Rank: Member

    Hi Jones,

    Thx for you reply.
    You suggestion is what I knew, doing the count in Process{}, and doing the real task in End{}.

    It will be appreciated that Microsoft can directly implement an variable of the pipeline objects count for Begin{}Process{}End{}.

    With this variable, for example my Wirte-Progress will be more beautiful because it will have the percentage information 🙂

  • #29735

    Topics: 6
    Replies: 236
    Points: 0
    Rank: Member

    Your kind request of Microsoft reflects a fundamental misunderstanding of how the pipeline works. As Don mentioned, the pipeline processes one object at a time as they arrive. At any given point in the pipeline, it has NO concept how much is still to come — it could be easily affected by varying input or filtering.

    So as much as you would like a pretty progress bar, it's not going to happen this way.

  • #29737

    Topics: 0
    Replies: 2
    Points: 0
    Rank: Member

    Thx Bob !
    So I won't display the percentage in write-progress.
    No choice.

  • #29740

    Topics: 0
    Replies: 8
    Points: 21
    Rank: Member

    If your intention is to show a process bar and use Write-Progress. You can keep a $TotalCount as well as a $ProcessedCount. You can use Write-Progress to show the growing number of items to be processed, then once you get to End{} The progress will show the percentage of the task "Completed"

    Depending upon what you're doing, and if your Process{} is filtering anything or doing any work other than counting. You may be able to show the percentage of completed vs. total piped in. This will be a moving target as the total will grow while the completed does not. It may take some getting used to seeing the percentage shrink as it goes on, but if you display the "completed / total" in the message it maye become clear.
    3 or 40 Completed...
    3 of 56 Completed...
    7 of 57 Completed...
    24 of 83 Completed...
    83 of 83 Comepleted... Done

The topic ‘count pipeline objects within Begin{}Process{}End{} blocks’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort