2

If I'm writing a binary PowerShell module and I happen to use a .NET library that writes to STDOUT (Console.Out) -- is there a recommended approach to reroute the console output to PowerShell's Output stream?

I can intercept the Console.Out by setting my own TextWriter via Console.SetOut(...) and then do something with it (i.e. I can buffer the output with a StringWriter and write it all out at some future point, or I can write a custom TextWriter that sends all Write commands to PowerShell's WriteObject).

My question is really, is there some standard or recommended practice for doing this or some built-in tools in the PowerShell library already?

E Bekker
  • 436
  • 6
  • 9

2 Answers2

0

I asked the question of the PS team via GitHub and the general response was that the TextWriter-to-WriteObject approach was a reasonable approach: https://github.com/PowerShell/PowerShell/issues/4849

E Bekker
  • 436
  • 6
  • 9
0

If you want to output to the pipeline use WriteObject(). WriteInformation(), WriteDebug(), WriteWarning() gets a message (string) to the host.

John Donnelly
  • 875
  • 1
  • 10
  • 29
  • Yes, that's fine for content that I control, but the point of the question was how to redirect someone else's output that goes to the STDIO output? In any even the solution was confirmed by the PS team -- you simply have to do the redirection yourself. – E Bekker Sep 20 '17 at 14:10