Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture error messages thrown by a command?

Tags:

powershell

I am writing a PowerShell script where in I need to capture the error message that it's throwing. Note: according to PowerShell, there is no error and command is executed successfully.

For example: I tried to download a package from SVN Link. The Link actually is not present. The script is showing me error message on the console. However, when I tried to check $_ or $? or $error, I did not see any error message. However, $LASTEXITCODE returned value 1. I need to get the exact error message.

like image 715
Avinash Ganesh Avatar asked Jul 02 '13 08:07

Avinash Ganesh


People also ask

How do I capture errors in PowerShell?

Use the try block to define a section of a script in which you want PowerShell to monitor for errors. When an error occurs within the try block, the error is first saved to the $Error automatic variable. PowerShell then searches for a catch block to handle the error.

Which command will copy error message to a file?

cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name.

Is there try catch in bash?

There is no try/catch in bash; however, one can achieve similar behavior using && or || . it stops your script if any simple command fails.

How do I get an exception message in PowerShell?

GetType(). FullName to view the exception message for the last error that occurred. Going back to the PowerShell console, rerun the New-Item command with a non-existent path, then run the $Error command to find the exception message.


2 Answers

If you get an error message, you need to capture the error stream:

$msg = command 2>&1

or

command 2>error.txt

PowerShell writes its messages to different streams that can be redirected to files for capturing the respective output.

  • Stream 1 (default): regular output ("STDOUT")
  • Stream 2: error messages ("STDERR"), including error messages from external programs
  • Stream 3: warning messages
  • Stream 4: verbose messages
  • Stream 5: debug messages
  • Stream 6: information messages (only PowerShell v5 and newer)

To capture a particular stream in a file you need to redirect the stream number to a file name. For instance

command 2>"C:\path\to\error.log"

would capture all error messages produced by command in the file C:\path\to\error.log. Use 2>> instead of 2> if you want to append to the file instead of overwriting it with each run.

You can also combine other streams with STDOUT to process/redirect all command output:

command >>"C:\path\to\all.log" *>&1

See Get-Help about_Redirection or this Scripting Guy article for more information about streams and redirection.

Things worth of note:

  • The *> redirection was introduced with PowerShell v3, hence it won't work in PowerShell v2 and earlier.
  • PowerShell v5 introduced a new stream (Information, stream number 6) since people kept misusing Write-Host because they didn't understand what the cmdlet was intended for.
like image 108
Ansgar Wiechers Avatar answered Oct 19 '22 09:10

Ansgar Wiechers


Assuming your executable is named svn.exe and is on the path, you can capture the messages it sends to console this way:

$msg = [string] (svn.exe <your parameters here>)

You can then parse the $msg string to find information you need.

like image 28
David Brabant Avatar answered Oct 19 '22 08:10

David Brabant