powershell 3+ try\catch error message output???
Posted on 2013-12-30
This code works, but no matter what I try I cannot get $error or $erroroutput to show the error messages. I can type them manually in the console\output sections of the PS IDE when the program stops to see what the errors are.
$CountCSV = Invoke-Sqlcmd -Query $Query -Username "test" -Password "test" -ErrorAction Stop -QueryTimeout 65535
$errorout += $error
#This next line outputs the text in the quotes, but I cannot get it to do anything else
Write-Error "CountCSV major error, query failed problem"
#this next line only shows the text in the quotes:
Write-Error "CountCSV major error, query failed problem" $errorout
#nothing here shows up at all
Again, I can type $error to see an error message and $errorout to see another error message. One is generic and the other is detailed. For example if there's a query time-out, one will basically say there's a networking issue while the other actual tells the query exceeded the timeout period. I would like to shows these 2 outputs automatically instead of having to type them to see what went wrong.
From what I've read on the Internet it should be working as expected, but it is acting like $error and $errorout are empty in the above example.
I've also used the same Try\Catch code in several invoke-sql sections of the program and none of them display the desired information.