5

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

krs
  • 543
  • 2
  • 17
  • 2
    Normally the return from [`Pipeline.Invoke`](http://msdn.microsoft.com/en-us/library/windows/desktop/system.management.automation.runspaces.pipeline.invoke(v=vs.85).aspx) is an enumerable over the output from the pipeline (for error output use the `Error` property of the same `Pipeline` instance). So please update your question with the code you are using to execute PSH pipelines in your host. – Richard Oct 11 '13 at 17:03
  • Hi, there are no `Error` in the pipeline at all. I cannot just simply `Pipeline.Invoke` as my CmdLets requires UI. Because of that doing it through custom `PSHost`, `PSHostUserInterface` where I can override user interaction and execute them. – krs Oct 14 '13 at 12:06

1 Answers1

2

I think Richard is onto it with his comment. If you are calling the cmdlets by using Pipeline.Invoke(), you have to either:

  • A) Call Pipeline.Invoke again with those objects and pipe them into Out-Default or
  • B) Just append the command Out-Default to 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-Process or in C# as output from Invoke(), 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-Default to the pipeline in this case.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Adding `Out-Default` helped. Hmmm, wondering why `Out-String` didn't helped... I also give a try to this "you are dealing with the actual .NET objects". Thank you very much, marking as an answer. – krs Oct 14 '13 at 12:13
  • When you use `Out-Default` PowerShell will render the objects to a string and then write to the host. With `Out-String` PowerShell will render the objects to a string and return that string as the pipeline output. It's up to you then to display it after it is returned from pipeline.Invoke(). – Keith Hill Oct 14 '13 at 14:02