Welcome › Forums › General PowerShell Q&A › Import-Module in Try Catch
- This topic has 2 replies, 2 voices, and was last updated 1 month, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
December 1, 2020 at 4:10 pm #275769
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?
-
December 1, 2020 at 5:43 pm #275796
Not sure how a try\catch would cause any issue. Would recommend removing the need to wrap try\catch using #Requires
PowerShell1234#Requires -Modules AzureAD, AzImport-Module -Name AzureADImport-Module -Name AzDo not have the Az module installed, so:
PowerShell12PS C:\Users\rasim> c:\Users\rasim\Desktop\temp.ps1ResourceUnavailable: The script 'temp.ps1' cannot be run because the following modules that are specified by the "#requires" statements of the script are missing: Az. -
December 4, 2020 at 12:49 pm #276624
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.
-
-
AuthorPosts
- You must be logged in to reply to this topic.