How to read PowerShell script stdout and stderr from C #

I am implementing my own PowerShell node and I need to read stdout and stderr from a PowerShell script. The problem is that I don't get stdout when I convert the object returned by calling the pipeline to a string. However, when I add the out-string cmdlet to the pipeline, it works fine. Is there any way to get stdout and stderror without using "out-string"?

this.currentPowerShell.AddScript(cmd);
Collection<PSObject> results = this.currentPowerShell.Invoke();
foreach (PSObject obj in results)
{
      Console.WriteLine(obj.ToString());
}

When I use the code above, I only get a partial stdout.

+3
source share
1 answer

PowerShell stdout exe . PowerShell native output - . , Out-String. # , PowerShell.

stderr ErrorRecords PowerShell.Streams.Error. , , Out-String .

+4

All Articles