Capturing PowerShell output in C # after each pipeline command

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) { // Create the PowerShell object. PowerShell powerShell = PowerShell.Create(); // If arguments were given, add the script and its arguments. if (args != null) { foreach (KeyValuePair<String, String> arg in args) { powerShell.AddScript(scriptText); powerShell.AddArgument(arg.Key); powerShell.AddArgument(arg.Value); } } // Otherwise, just add the script. else powerShell.AddScript(scriptText); // Add the event handlers. PSDataCollection<PSObject> output = new PSDataCollection<PSObject>(); output.DataAdded += new EventHandler<DataAddedEventArgs>(Output_DataAdded); powerShell.InvocationStateChanged += new EventHandler<PSInvocationStateChangedEventArgs>(Powershell_InvocationStateChanged); // Invoke the pipeline asynchronously. IAsyncResult asyncResult = powerShell.BeginInvoke<PSObject, PSObject>(null, output); } private void Output_DataAdded(object sender, DataAddedEventArgs e) { PSDataCollection<PSObject> myp = (PSDataCollection<PSObject>)sender; Collection<PSObject> results = myp.ReadAll(); foreach (PSObject result in results) { Console.WriteLine(result.ToString()); } } 

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.

+4
source share
2 answers

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

I would still be interested to know whether it is possible to produce output after each command in the pipeline.

+3
source

I have a situation where I have an object that manages the PowerShell environment, and it takes a script or module and cmdlet and executes it, and then takes a new script or module and a cmdlet or cmdlet from the original module and executes it. Every time something is executed, I need to return the results. I solved this by clearing the command queue after each execution:

 powerShellInstance.Commands.Clear(); 

hope this helps.

0
source

All Articles