@@ -39,31 +39,58 @@ param (
39
39
[switch ] $Force
40
40
)
41
41
42
- function CheckModule ($m ) {
43
42
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
46
50
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
+ }
51
67
}
52
68
else {
53
69
54
70
# 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
+ }
58
83
}
59
84
else {
60
85
61
86
# 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."
63
88
EXIT 1
64
89
}
65
90
}
66
91
}
92
+
93
+ return $retVal
67
94
}
68
95
69
96
function ConvertTo-Hashtable {
@@ -117,7 +144,7 @@ $requiredModules = @(
117
144
" Az.ConnectedMachine" ,
118
145
" Az.ResourceGraph"
119
146
)
120
- $requiredModules | Foreach-Object {CheckModule $_ }
147
+ $requiredModules | Foreach-Object {LoadModule $_ }
121
148
122
149
# Subscriptions to scan
123
150
0 commit comments