Capturing PowerShell output in C# after execution of each pipeline command

Question:

I am implementing a WPF application that executes a PowerShell script for each key/value pair given in a dictionary, using the pair as script arguments. I store each run of the script as a new command in the pipeline. However, this causes me to only get output back from the last command that was run, when I need the output after each run of the script. I have considered creating a new pipeline each time the script is executed, but I need to know when all executions of the script are done. Here’s the relevant code to help explain my problem:

And then I use the following method to know when all executions of the script have been completed. Since I do this by checking that the invocation state of the pipeline is completed, I can’t make a new pipeline for each execution of the script:

So, to get to my question:

1) Can I force the pipeline to produce output after each command that it executes? Or,
2) If I were to create a new pipeline each time I run the command, is there another way that I could check that all executions of the script have been completed?

There are few examples using the actual PowerShell class in C# and I know next to nothing about threading, so any help would be greatly appreciated.

Answer:

I feel silly for answering my own question, but all I did was move the loop functionality from my C# code into my script and that worked. So now I pass all the keys and values at once as array parameters and only have the one command in the pipeline.

I would still be interested to know if it is possible to produce output after each command in the pipeline is executed, though.

Source:

Capturing PowerShell output in C# after execution of each pipeline command by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply