user32882
user32882

Reputation: 5917

Standard output in powershell debug console

I have a long script in powershell which calls an even longer function located in a separate .ps1 file. The function runs some svn update commands and some compiled executables which produce standard output. When I run these directly from the script the output gets redirected to the debug console in Powershell ISE. When I run them through the function I can tell they are running but I get no standard output in the console.

How do I redirect standard output from my function back to the powershell debug console where I can see it?

THanks.

EDIT

I am importing the function as follows:

. "D:\common.ps1"

and calling it as follows:

$MedianValue = Run-Comparison $LastRev $FirstRev $ScriptPath $SolutionPath $DevenvPath $TestPath $refFile $SVNPAth

Within the function, one of the calls is as follows

svn update $FirstRev Start-Process ExecutableName Argument

It is for the above two statements that I cannot see the standard output for when I call their containing function.

Upvotes: 1

Views: 797

Answers (1)

mklement0
mklement0

Reputation: 440347

If you're capturing a script's / function's output and that script / function contains a mix of PowerShell-native output statements and external-program calls producing stdout output, both types of output are sent to PowerShell's regular success output streams.

Therefore, unless you redirect at the source, you cannot selectively pass stdout from external programs through to the the host (e.g., a regular console window or the console pane in the ISE), because you won't be able to tell which output objects (lines) come from where.

To redirect at the source - if you have control over the callee's source code - you have several options, the simplest being Write-Host, as the following example demonstrates:

function Run-Comparison {
  'PS success output'
  cmd /c 'echo external stdout output' | Write-Host
}

# Captures 'PS success output', but passes the cmd.exe output through to the console.
$MedianValue = Run-Comparison

The above selectively sends the cmd.exe command's output to the host.

In PSv5+, where Write-Host writes to the newly introduced information stream (number 6), you can optionally suppress the to-host output with 6>$null on invocation.

To reverse the logic, use Write-Information instead of Write-Host (PSv5+ only), which is silent by default and allows you to turn on output with $InformationPreference = 'Continue'.

If you want silent-by-default behavior in PSv4-, use Write-Verbose or Write-Debug, but note that such output will be a different color, with each line having a prefix (VERBOSE: and DEBUG:, respectively).

Upvotes: 1

Related Questions