![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | handling errors in a script If I'm calling a cmdlet in a PSh script, what is the best way to trap for errors in the cmdlet? In other words, I call the cmdlet then do a bunch of things afterwards. I'd like to script to bail out if the cmdlet throws an exception. I'm sure this is easy, but just looking for the "right" approach. Thanks! Darren |
My System Specs![]() |
| | #2 (permalink) |
| | Re: handling errors in a script Place the trap before the call to the Cmdlet. '$_' holds the error information. Use exit in the trap to exit the script. If the error is a non-terminating error set the Cmdlet's -ErrorAction parameter to Stop or 1: Try these sample scripts: -< test1.ps1 >- trap { 'Exception trapped!' $_ exit } # non-terminating error, ignored by trap Get-Content nosuchdir # terminating error Get-Date 'Monday' "last line, should not be displayed" -< end >- # - - - - - - - - # -< test2.ps1 >- trap { 'Exception trapped!' $_ exit } # set -ErrorAction to Stop # on non-terminating error Get-Content nosuchdir -ea 1 # terminating error Get-Date 'Monday' "last line, should not be displayed" -< end >- -- Kiron |
My System Specs![]() |
| | #3 (permalink) |
| | Re: handling errors in a script If you are used to try-catch-finally functionality and would prefer to use that in your scripts, you should read this blog post: http://weblogs.asp.net/adweigert/arc...s-to-life.aspx -- Kirk Munro Poshoholic http://poshoholic.com "Kiron" <Kiron@xxxxxx> wrote in message news:C686D8FD-E416-4DD7-BFEF-2C78AAF1789B@xxxxxx Quote: > Place the trap before the call to the Cmdlet. '$_' holds the error > information. Use exit in the trap to exit the script. If the error is a > non-terminating error set the Cmdlet's -ErrorAction parameter to Stop or > 1: > > Try these sample scripts: > > -< test1.ps1 >- > trap { > 'Exception trapped!' > $_ > exit > } > # non-terminating error, ignored by trap > Get-Content nosuchdir > > # terminating error > Get-Date 'Monday' > > "last line, should not be displayed" > -< end >- > > # - - - - - - - - # > > -< test2.ps1 >- > trap { > 'Exception trapped!' > $_ > exit > } > # set -ErrorAction to Stop > # on non-terminating error > Get-Content nosuchdir -ea 1 > > # terminating error > Get-Date 'Monday' > > "last line, should not be displayed" > -< end >- > > -- > Kiron |
My System Specs![]() |
| | #4 (permalink) |
| | Re: handling errors in a script Thanks Kirk and Kiron. Very useful info. Darren "Kirk Munro" <sorry@xxxxxx> wrote in message news:Osye$MsMIHA.292@xxxxxx Quote: > If you are used to try-catch-finally functionality and would prefer to use > that in your scripts, you should read this blog post: > > http://weblogs.asp.net/adweigert/arc...s-to-life.aspx > > -- > Kirk Munro > Poshoholic > http://poshoholic.com > > > "Kiron" <Kiron@xxxxxx> wrote in message > news:C686D8FD-E416-4DD7-BFEF-2C78AAF1789B@xxxxxx Quote: >> Place the trap before the call to the Cmdlet. '$_' holds the error >> information. Use exit in the trap to exit the script. If the error is a >> non-terminating error set the Cmdlet's -ErrorAction parameter to Stop or >> 1: >> >> Try these sample scripts: >> >> -< test1.ps1 >- >> trap { >> 'Exception trapped!' >> $_ >> exit >> } >> # non-terminating error, ignored by trap >> Get-Content nosuchdir >> >> # terminating error >> Get-Date 'Monday' >> >> "last line, should not be displayed" >> -< end >- >> >> # - - - - - - - - # >> >> -< test2.ps1 >- >> trap { >> 'Exception trapped!' >> $_ >> exit >> } >> # set -ErrorAction to Stop >> # on non-terminating error >> Get-Content nosuchdir -ea 1 >> >> # terminating error >> Get-Date 'Monday' >> >> "last line, should not be displayed" >> -< end >- >> >> -- >> Kiron > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: handling errors in a script "Darren Mar-Elia" <dmanonymous@xxxxxx> wrote in message news:2B668D38-8B7F-434F-B23A-2F0967D18CDB@xxxxxx Quote: > If I'm calling a cmdlet in a PSh script, what is the best way to trap for > errors in the cmdlet? In other words, I call the cmdlet then do a bunch of > things afterwards. I'd like to script to bail out if the cmdlet throws an > exception. I'm sure this is easy, but just looking for the "right" > approach. > enforce that if the cmdlet errors, the error is a terminating error instead of a non-terminating error. To do this, use the ubiqitous parameter -ErrorAction and set it to Stop e.g.: Get-ChildItem someNameThatDoesntExist -ErrorAction Stop This will cause script execution to jump to the previous trap handler in scope and on up the call stack. If none exists the script will exit automatically with that error. Another option, if you want this behavior for all cmdlet calls within a certain scope, is to set $ErrorActionPreference = 'Stop' before the cmdlet invocations. -- Keith |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Strategy for handling parameters to a script | PowerShell | |||
| Script errors | Vista mail | |||
| script errors | Vista Games | |||
| IE Script Errors | Vista General | |||
| Handling Errors from COM components | PowerShell | |||