1

I'm working at the moment with a PowerShell script calling a C# console application, but I'm curious if there's a general solution.

Basically, my script loops over some files, prints out something for each one, and calls my program. Everything works, but what I'd like to do is have my console program's output show up indented by one tab stop. Right now I've managed that manually (just "hard-coding" the tab stop in my Console.WriteLine() statements), but that's hardly ideal from a maintenance standpoint, since sometimes this program will be run on its own.

An example of what I'm looking for is

powershell output file #1
    c# program output
    c# program output

powershell output file #2
    c# program output
    c# program output

Would it help to use a dedicated logger class? It would also be slick to do different colors for the called program, but I'd settle for a non-intrusive way to do indentation. In particular, a solution that avoids the necessity of editing the called program (e.g. for third party code I don't have source for) would be preferred.

Jay Carlton
  • 1,118
  • 1
  • 11
  • 27
  • 1
    Related - http://stackoverflow.com/questions/2547428/net-console-textwriter-that-understands-indent-unindent-indentlevel – ChrisF Dec 05 '15 at 22:25

1 Answers1

1

Here's a pure PS solution:

# This assumes all output you care about is via stdout
$yourProgramOutput = yourprogram.exe
$yourProgramOutput | foreach-object {
    write-host "`t$_"
}

or as a one-liner:

yourprogram | % { write-host "`t$_"}
Χpẘ
  • 3,403
  • 1
  • 13
  • 22
  • I like it. How does it handle wrapped lines from the console program? – Jay Carlton Dec 06 '15 at 00:34
  • It really depends on the "Powershell host". But the host in a console session will wrap the lines even more than without the tab. In the Powershell ISE, the lines don't wrap - you use the horizontal scroll bar to see the part past the edge of the window. Note your question doesn't mention anything about wrapping. If you have a new question about wrapping, please create a new post. By the way, you can accept the answer if you find it, well, acceptable. – Χpẘ Dec 06 '15 at 00:41