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


This topic contains 5 replies, has 5 voices, and was last updated by Profile photo of Schlauge Schlauge 1 year, 1 month ago.

  • Author
  • #29731
    Profile photo of Xiang ZHU
    Xiang ZHU


    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
    Profile photo of Don Jones
    Don Jones

    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
    Profile photo of xiang zhu
    xiang zhu

    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
    Profile photo of Bob McCoy
    Bob McCoy

    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
    Profile photo of xiang zhu
    xiang zhu

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

  • #29740
    Profile photo of Schlauge

    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

You must be logged in to reply to this topic.