0

I have a task that requires me to collect an array, refine it by removing junk entries, and finally process each line. Breaking each of those tasks into functions is fine, but setting up the script to run them ends up looking like this:

$items = Get-ItemsFromSource
$items = Remove-JunkItems $items
$items = Process-Items $items
Report-Progress $items

Or I could do it in one line with nested function calls:

Report-Progress (Process-Items (Remove-JunkItems (Get-ItemsFromSource)))

But what I would like to do is the easier-to-read (for me, at least) sequence of calls that one can enjoy with pipelines:

Get-ItemsFromSource | Remove-JunkItems | Process-Items | Report-Progress

My issue is that the list of items aren't independent of each other: The Remove-JunkItems step requires that I check the full set of items to determine if each is a junk item or not. My current Remove-JunkItems works fine right now, but it isn't going to work if I process the items individually, which is what I'm getting when making use of the [Parameter(ValueFromPipeline)] attribute.

Is there a way?

6
  • 2
    unless you have left out something, there is no need to process EVERYTHING in the Remove-JunkItems step. just process one item at a time and accumulate the filtered collection at the end of the pipeline. that is what a pipeline is usually about - one thing at a time. Commented Sep 26 at 10:40
  • Maybe sharing example code of commands would help? Or at least minimal example. Commented Sep 26 at 11:12
  • 2
    This should answer your question: stackoverflow.com/questions/75978956/…. Basically you need to collect all items in the process block then act on it in the end block Commented Sep 26 at 11:37
  • @SantiagoSquarzon - Thanks! This is a good match for what I ended up doing. Commented Oct 9 at 8:51
  • @RLDailey - As I mentioned in the OP, I do need to process everything, each item needs to be examined in relation to the greater context, not just independently. In this case it's not merely the task that was misunderstood. :) Commented Oct 9 at 8:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.