How do I suppress standard error output in PowerShell?

China☆狼群 提交于 2019-12-18 12:56:08

问题


In a PowerShell script automating some SVN tasks I have the following function:

function SvnUrlExists($url)
{
  svn info $url | out-null 2>&1
  return $?
}

Since this explicitly tests whether some SVN repository URL exists, I am not interested at all in any error output. However, despite everything I found about redirecting stderr in PowerShell suggesting 2>&1 to redirect it to stdout, this still outputs an error message:

svn: warning: W170000: URL 'blahblah' non-existent in revision 26762
svn: E200009: Could not display info for all targets because some targets don't exist

Unfortunately, this severely messes up the output of my script.

What am I doing wrong, and how should I suppress this error output?


回答1:


Just in case someone else googles for similar terms as I did:

After I have been banging my forehead against this for hours, of course I found the solution within minutes after posting the question here:

svn info $url 2>&1 | out-null

This works like a charm.




回答2:


If you want to suppress only standard error, use:

svn info $url 2>$null



回答3:


One can also do this:

svn info $url *> $null

See Suppress console output in PowerShell

about_Redirection




回答4:


The voted answer generates an error for me. The solution ended up being the following:

cmd /c "MyProgram MyArguments 2>&1" | Out-Null



回答5:


tl;dr

function SvnUrlExists($url)
{

  # Suppress all output streams (external stdout and stderr in this case)
  # To suppress stderr output only, use 2>$null
  svn info $url *>$null 

  # For predictable results with external programs, 
  # infer success from the *process exit code*, not from the automatic $? variable.
  # See https://github.com/PowerShell/PowerShell/issues/10512
  $LASTEXITCODE -eq 0
}

Your own answer effectively addresses the redirection issue.

Steven Penny's answer proposes *>$null, i.e. suppressing all output streams as a convenient alternative - it obviates the need for Out-Null, which I generally suggest replacing with $null = ... - see this answer.

However, there's another problem with the code in your question:

While it may work with your particular command, $?, unfortunately, is not a robust indicator of whether an external program succeeded or not - use $LASTEXITCODE -eq 0 instead, because - due to a bug as of PowerShell Core 7.0.0-preview.3, reported on GitHub here - $? can end up reflecting $false even when $LASTEXITCODE is 0 (unequivocally signaling success).


As for what you tried in your question:

svn info $url | out-null 2>&1  # WRONG
  • Only success output is sent through to the pipeline (stdout output from external programs is sent to PowerShell's success output stream).

  • Redirections such as 2>&1 act on individual commands in a pipeline, not the pipeline as a whole.

  • Therefore, if svn info $url produces stderr output, it prints straight to the host (console) - Out-Null never sees it.



来源:https://stackoverflow.com/questions/11969596/how-do-i-suppress-standard-error-output-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!