@@ -39,31 +39,58 @@ param (
3939 [switch ] $Force
4040)
4141
42- function CheckModule ($m ) {
4342
44- # This function ensures that the specified module is imported into the session
45- # If module is already imported - do nothing
43+ function LoadModule
44+ {
45+ param (
46+ [parameter (Mandatory = $true )][string ] $name
47+ )
48+
49+ $retVal = $true
4650
47- if (! (Get-Module | Where-Object {$_.Name -eq $m })) {
48- # If module is not imported, but available on disk then import
49- if (Get-Module - ListAvailable | Where-Object {$_.Name -eq $m }) {
50- Import-Module $m
51+ if (! (Get-Module - Name $name ))
52+ {
53+ $retVal = Get-Module - ListAvailable | Where-Object {$_.Name -eq $name )
54+
55+ if ($retVal )
56+ {
57+ try
58+ {
59+ Import-Module $name - ErrorAction SilentlyContinue
60+ }
61+ catch
62+ {
63+ write-host " The request to lload module $ ( $name ) failed with the following error:"
64+ write-host $_.Exception.Message
65+ $retVal = $false
66+ }
5167 }
5268 else {
5369
5470 # If module is not imported, not available on disk, but is in online gallery then install and import
55- if (Find-Module - Name $m | Where-Object {$_.Name -eq $m }) {
56- Install-Module - Name $m - Force - Verbose - Scope CurrentUser
57- Import-Module $m
71+ if (Find-Module - Name $name ) {
72+ Install-Module - Name $name - Force - Verbose - Scope CurrentUser
73+ try
74+ {
75+ Import-Module $name - ErrorAction SilentlyContinue
76+ }
77+ catch
78+ {
79+ write-host " The request to lload module $ ( $name ) failed with the following error:"
80+ write-host $_.Exception.Message
81+ $retVal = $false
82+ }
5883 }
5984 else {
6085
6186 # If module is not imported, not available and not in online gallery then abort
62- write-host " Module $m not imported, not available and not in online gallery, exiting."
87+ write-host " Module $ ( $name ) not imported, not available and not in online gallery, exiting."
6388 EXIT 1
6489 }
6590 }
6691 }
92+
93+ return $retVal
6794}
6895
6996function ConvertTo-Hashtable {
@@ -117,7 +144,7 @@ $requiredModules = @(
117144 " Az.ConnectedMachine" ,
118145 " Az.ResourceGraph"
119146)
120- $requiredModules | Foreach-Object {CheckModule $_ }
147+ $requiredModules | Foreach-Object {LoadModule $_ }
121148
122149# Subscriptions to scan
123150
0 commit comments