Windows Vista Forums

handling errors in a script
  1. #1


    Darren Mar-Elia Guest

    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 SpecsSystem Spec

  2. #2


    Kiron Guest

    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 SpecsSystem Spec

  3. #3


    Kirk Munro Guest

    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

    > 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 SpecsSystem Spec

  4. #4


    Darren Mar-Elia Guest

    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

    > 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

    >> 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 SpecsSystem Spec

  5. #5


    Keith Hill [MVP] Guest

    Re: handling errors in a script

    "Darren Mar-Elia" <dmanonymous@xxxxxx> wrote in message
    news:2B668D38-8B7F-434F-B23A-2F0967D18CDB@xxxxxx

    > 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.
    >
    It sounds like to me that you don't need to trap errors. You just want to
    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 SpecsSystem Spec

handling errors in a script problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strategy for handling parameters to a script Jacob Saaby Nielsen PowerShell 3 27 Jun 2008
Script errors Bob from down under Vista mail 1 11 Jun 2008
script errors butch Vista Games 1 25 Jan 2008
IE Script Errors Don Bouchard Vista General 1 17 Aug 2007
Handling Errors from COM components peterchen PowerShell 0 17 Oct 2006