Reputation: 453
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.
Upvotes: 12
Views: 64299
Reputation: 200233
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.
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:
*>
redirection was introduced with PowerShell v3, hence it won't work in PowerShell v2 and earlier.Write-Host
because they didn't understand what the cmdlet was intended for.Upvotes: 35
Reputation: 8541
Inspired by David Brabants answer, you can combine both stdout and stderr into one string array using this command:
$output = [string[]] (.\Cli.exe -p $param 2>&1)
It will execute Cli.exe
with the parameter p
. This is optionally.
2>&1
means the stream #2 (stderr) will be redirected to stream #1 (stdout), thus both streams will arrive in $output
.
Upvotes: 2
Reputation: 43489
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.
Upvotes: 6