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?
If there are special commands you want to ignore you can use -erroraction 'silentlycontinue' which will basically ignore all error messages generated by that command. You can also use the Ignore value (in PowerShell 3+): Unlike SilentlyContinue, Ignore does not add the error message to the $Error automatic variable.
What to do if you want to clean out all the entries in $error? $error is a variable, so you can try with the Clear-Variable cmdlet: PS> Clear-Variable error -Force Clear-Variable : Cannot overwrite variable Error because it is read-only or constant.
To catch the error you will need toadd the -ErrorAction Stop parameter behind your action. Another option is to change the ErrorActionPreference at the beginning of your script or PowerShell session. But keep in mind that the preference will reset to continue when you start a new session.
If you want to suppress only standard error, use:
svn info $url 2>$null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With