powershell 3+ try\catch error message output???

Posted on 2013-12-30
Last Modified: 2013-12-31
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.
Any ideas?
$CountCSV = Invoke-Sqlcmd -Query $Query -Username "test" -Password "test" -ErrorAction Stop -QueryTimeout 65535  
    $errorout += $error[0]
#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
            write-host $errorout

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.
Question by:johnj_01201
  • 2
  • 2
LVL 39

Assisted Solution

footech earned 250 total points
ID: 39747086
Write-Error "CountCSV major error, query failed problem" $errorout
won't work.  In fact that should generate an error.  You can only give it one message to display.
In testing it appears your problem (that $error and $errorout don't output anything in the catch block) is caused by this issue.  Can't say for certain, but maybe it's because PS doesn't handle errors within a catch block well.  That's pure speculation on my part, since I don't know how PS handles errors within a catch block, but it seems to fit the behavior.

Is $errorout defined earlier as an empty array (e.g. $errorout = @() )?
If not then there could be errors when trying to add to it with +=.

Write-Host really only works to output a string, not the entire object, so be conscious of what you output with it.  If you try to output an object with it, it will try to convert it to a string, so that could explain the difference you're seeing between the error messages.
To start off with error handling, I probably wouldn't recommend using Write-Error to begin with.
LVL 68

Accepted Solution

Qlemo earned 250 total points
ID: 39747824
I've used Write-Error in just for demonstration purposes for that specific test case - because you told the recursion error does not appear, which it does. It generates a new error, and if errors in a catch block occur execution is halted. (You can, of course, nest try/catch to achieve error handling in error handling.)

Keep in mind $error contains all errors (exceptions) which were triggered in the PS session's lifetime, unless $error.Clear() is called. Dumping that var will always supply more info than using only the most recent entry, $error[0] (which is, btw, the same $_ in the catch block).

Also be aware that an object of $error is of type ErrorRecord and contains much more than only an error message. That should be used for proper handling and reporting. And that again requires that you think carefully about what should happen in each particular case.
The common methods are to collect error information in a var (as you did with $errorout), and dump that later.
Or check the  error category, and stop the script, e.g. with the Write-Error cmdlet.

Author Closing Comment

ID: 39748177
Thanks. I guess the try\catch is not as simple as I thought. I will research and learn more about it.
LVL 68

Expert Comment

ID: 39748547
Proper error handling is indeed a more advanced topic ...
LVL 39

Expert Comment

ID: 39749073
I can't remember where I've read it, but I believe I've seen it said a few times that error handling can easily double the size of your script, and take twice as long to write as the main script portion (or more).

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now