Background
- PowerShell 3 with SDK
- C# with Framework 4.5
Implementation
I have implemented my own PSHost alogn with PSHostUserInterface. In PSHostUserInterface I override all Write... methods and colleting the to one variable which serves the output.
Problem
In my application I calling Cmdlets which use WriteObject as their output. In the PSHostUserInterface.Write... methods I am getting everything but these WriteObject's output. For example, I see this in regular PowerShell:
This is sample string output from the command
Here we have object from Cmdlet.WriteObject function
This is another string output from the command
This is what I get in my custom PSHost in my application:
This is sample string output from the command
This is another string output from the command
Question
How can I get in C# all the outputs of the Cmdlet?
Many thanks
I think Richard is onto it with his comment. If you are calling the cmdlets by using
Pipeline.Invoke(), you have to either:Out-DefaultorOut-Defaultto the original pipeline. This will tell PowerShell to send the output to the display (and use the default formatting).Normally, when you stash the resulting objects that are output by a pipeline, either in a PowerShell variable
$res = Get-Processor in C# as output fromInvoke(), you are dealing with the actual .NET objects. The formatting & rendering of those objects to the host is another step that PowerShell will do for you if the pipeline output is not captured. PowerShell effectively appends a| Out-Defaultto the pipeline in this case.