Solved

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

Posted on 2013-12-30
5
773 Views
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?
Try
{
$CountCSV = Invoke-Sqlcmd -Query $Query -Username "test" -Password "test" -ErrorAction Stop -QueryTimeout 65535  
}
catch{
    $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
$error
$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.
0
Comment
Question by:johnj_01201
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40

Assisted Solution

by:footech
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.
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 250 total points
ID: 39747824
I've used Write-Error in http://www.experts-exchange.com/Q_28325801.html 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.
0
 
LVL 1

Author Closing Comment

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

Expert Comment

by:Qlemo
ID: 39748547
Proper error handling is indeed a more advanced topic ...
0
 
LVL 40

Expert Comment

by:footech
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).
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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