1

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.

3
  • Yeah that talks about redirecting to text files. I need to redirect to console... Commented Jun 30, 2018 at 18:44
  • Well after thinking aboout it, the interaction with the ISE and redirecting normal/success out won't work. Your best option might be integrating write-debug commands in the common.ps1 --> stackoverflow.com/a/45289842/6811411 Commented Jun 30, 2018 at 19:33
  • Could you take a look at the two commands I added in my edit? Those are the ones I need to see the output for... Can I use Write-Debug on those? Commented Jun 30, 2018 at 20:39

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Your answer is very close to being satisfactory. Write-Host works for svn and devenv type commands inside my function. I am now getting some of the desired output. For one last command, however, I tried Start-Process -FilePath "Exec.exe" -ArgumentList "test" -Wait | Write-Host, and it still opens up a new cmd window. I also tried -NoNewWindow but then the command fails claiming it cannot find the file.
@user32882: Try -NoNewWindow in combination with -FilePath ($PWD.ProviderPath + '\Exec.exe') (and no piping to Write-Host). However, if Exec.exe is a console application, why don't you just invoke it directly, just like svn and devenv? .\Exec.exe test | Write-Host
@user32882: Glad to hear it; my pleasure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.