I am implementing a WPF application that runs a PowerShell script for each key / value pair specified in a dictionary, using a pair as script arguments. I save each script run as a new command in the pipeline. However, this leads to the fact that I only get the output from the last command executed, when I need the output after each script run. I considered creating a new pipeline every time a script is executed, but I need to know when all script executions are executed. Here is the relevant code to help explain my problem:
private void executePowerShellScript(String scriptText, Dictionary<String, String> args) {
And then I use the following method to find out when all script executions have been completed. Since I do this by checking that the state of the pipeline call is complete, I cannot create a new pipeline for each script execution:
private void Powershell_InvocationStateChanged(object sender, PSInvocationStateChangedEventArgs e) { switch (e.InvocationStateInfo.State) { case PSInvocationState.Completed: ActiveCommand.OnCommandSucceeded(new EventArgs()); break; case PSInvocationState.Failed: OnErrorOccurred(new ErrorEventArgs((sender as PowerShell).Streams.Error.ReadAll())); break; } Console.WriteLine("PowerShell object state changed: state: {0}\n", e.InvocationStateInfo.State); }
So, to answer my question:
1) Can I get the pipeline to output after every command it executes? Or, 2) If I had to create a new pipeline every time I run the command, is there another way to check that all script executions are complete?
There are some examples of using the actual PowerShell class in C #, and I know almost nothing about streaming, so any help would be greatly appreciated.
Casey source share