Import-Module in Try Catch

Welcome Forums General PowerShell Q&A Import-Module in Try Catch

Viewing 2 reply threads
  • Author
    Posts
    • #275769
      Participant
      Topics: 3
      Replies: 26
      Points: 106
      Helping Hand
      Rank: Participant

      I have a machine with the modules AzureAD and Az installed.

      Executing Import-Module AzureAD or Import-Module Az both work OK from within Powershell ISE.

      When putting Import-Module AzureAD inside a Try Catch it still works, however putting Import-Module Az inside a Try Catch blocks the ISE (I have to close it or end the statement with Ctrl-C).

      My question is: what can cause the import to fail in this way? Why only with the Az module and why only inside a Try Catch?

    • #275796
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      Not sure how a try\catch would cause any issue. Would recommend removing the need to wrap try\catch using #Requires

      Do not have the Az module installed, so:

    • #276624
      Participant
      Topics: 3
      Replies: 26
      Points: 106
      Helping Hand
      Rank: Participant

      Hello Rob,

      Thanks for the suggestion. The problem is that I need to be able to react when the module is not yet installed. The #Requires statement prevents the script from running if the module isn’t there.

      But thanks anyway,

      Kris.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.