Try/Catch block not working as expected

This topic contains 5 replies, has 2 voices, and was last updated by  Kevyn 3 months ago.

  • Author
    Posts
  • #75485

    Kevyn
    Participant

    I'm using Windows PowerShell 5.1 and am running the following code in my script. If there is an error trying to import the ActiveDirectory module, I want to display an error message and then exit the script so that nothing else tries to execute. However, the Catch block is not being executed. If I put some nonsense word like "blah" in place of the Install-Module command, the Catch block executes. From reading the help on the Try/Catch/Finally block (help about_try_catch_finally -full), it talks about "terminating" errors. I'm guessing that the error I'm getting is not considered a "terminating" error.

    Code Snippet:

    Try
    {
      Import-Module ActiveDirectory
    }
    Catch
    {
      Write-Error "The module failed to load.  Install the module and try again."
      Exit
    }
    

    I get the following error and additional errors since the rest of my code doesn't execute properly.

    Import-Module : The specified module 'activedirectory' was not loaded because no valid module file was found in any module directory.
    At line:1 char:6
    + $a = Import-Module activedirectory
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ResourceUnavailable: (activedirectory:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

    I've found the workaround below, but am curious if the Try/Catch should work in this instance and what, exactly, defines a "terminating" error. The help for about_try_catch_finally just says the following about "terminating" errors:

    "A terminating error stops a statement from running. If Windows PowerShell
    does not handle a terminating error in some way, Windows PowerShell also
    stops running the function or script using the current pipeline. In other
    languages, such as C#, terminating errors are referred to as exceptions.
    For more information about errors, see about_Errors."

    If I run Help about_errors -Full, I just get the help for about_try_catch_finally again.

    Workaround:

    $ADModuleImported = Import-Module ActiveDirectory
    If($ADModuleImported -eq $Null)
    {
      Write-Warning "The ActiveDirectory module failed to load.  Install the module and try again."
      Exit
    }
    

    Don Jones/Jeffrey Hicks: In chapter 26 (Title: Using Someone Else's Script) of your "Learn Windows Powrshell In A Month Of Lunches" book, You have a Try/Catch block for an attempted import of a module, and your analysis of the script (Page 320) afterwards indicates the Catch block should execute through you say that it would be better to just exit the code, which is what I'm trying to do, though my code is in a script, not a function.

  • #75487

    Olaf Soyk
    Participant

    If you use a try catch block you have to set the "-ErrorAction Stop" for the cmdlet in the try block.

    • #75494

      Kevyn
      Participant

      Thanks. That allowed the Catch block to execute. However, the error didn't get written out to the screen. Also, in Don's and Jeffrey's book, they don't have the -ErrorAction parameter in the Install-Module command.

    • #75500

      Olaf Soyk
      Participant

      I don't know what to say ... it works just as expected in my environment.

    • #75499

      Kevyn
      Participant

      I came up with what I feel is a better workaround than what I provided earlier.

      $Error.Clear()
      Import-Module ActiveDirectory
      If($Error -ne $Null)
      {
        Write-Warning "The ActiveDirectory module failed to load.  Resolve the issue and try again."
        Exit
      }
      
  • #75503

    Kevyn
    Participant

    Thank Olaf. I appreciate the input.

You must be logged in to reply to this topic.