Answered by:
powershell - Try/Catch and external exe (like sqlcmd)

-
Hello,
How Can I catch errors from exetrenal programs like sqlcmd, bcp, and other tools.
I'd like catch error (generally sql server error)
Msg 102, Level 15, State 1, Server BILL2K8, Line 1 Incorrect syntax near 'SSSSSSselect' .
and write it to some text log file, bellow block does't catch error:
try { sqlcmd -E -SBILL2K8 -Q "set nocount on; SSSselect @@version;" -b -h-1 } catch { Write-Output "ERR!!!" | Out-File .\error_log.txt }
Question
Answers
-
PowerShell can't deal with errors from command-line EXEs. You should get $LASTEXITCODE after running the EXE to determine the return code and deal with it appropriately.
- Marked as answer by Marco ShawModerator Wednesday, October 20, 2010 9:10 PM
All replies
-
The Try/Catch only catches terminating errors. Before the Try/Catch, add this: $erroractionpreference = 'stop'. This way if any error occurs, it will be treated as a terminating error.
-
-
PowerShell can't deal with errors from command-line EXEs. You should get $LASTEXITCODE after running the EXE to determine the return code and deal with it appropriately.
- Marked as answer by Marco ShawModerator Wednesday, October 20, 2010 9:10 PM
-