@@ -39,33 +39,6 @@ param (
39
39
[switch ] $Force
40
40
)
41
41
42
- function CheckModule ($m ) {
43
-
44
- # This function ensures that the specified module is imported into the session
45
- # If module is already imported - do nothing
46
-
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
- }
52
- else {
53
-
54
- # 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
58
- }
59
- else {
60
-
61
- # 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."
63
- EXIT 1
64
- }
65
- }
66
- }
67
- }
68
-
69
42
function ConvertTo-Hashtable {
70
43
[CmdletBinding ()]
71
44
[OutputType (' hashtable' )]
@@ -105,6 +78,60 @@ function ConvertTo-Hashtable {
105
78
}
106
79
}
107
80
81
+ # This function checks if the specified module is imported into the session and if not installes and/or imports it
82
+ function LoadModule
83
+ {
84
+ param (
85
+ [parameter (Mandatory = $true )][string ] $name
86
+ )
87
+
88
+ $retVal = $true
89
+
90
+ if (! (Get-Module - Name $name ))
91
+ {
92
+ $retVal = Get-Module - ListAvailable | Where-Object {$_.Name -eq $name )
93
+
94
+ if ($retVal )
95
+ {
96
+ try
97
+ {
98
+ Import-Module $name - ErrorAction SilentlyContinue
99
+ }
100
+ catch
101
+ {
102
+ write-host " The request to lload module $ ( $name ) failed with the following error:"
103
+ write-host $_.Exception.Message
104
+ $retVal = $false
105
+ }
106
+ }
107
+ else {
108
+
109
+ # If module is not imported, not available on disk, but is in online gallery then install and import
110
+ if (Find-Module - Name $name ) {
111
+ Install-Module - Name $name - Force - Verbose - Scope CurrentUser
112
+ try
113
+ {
114
+ Import-Module $name - ErrorAction SilentlyContinue
115
+ }
116
+ catch
117
+ {
118
+ write-host " The request to lload module $ ( $name ) failed with the following error:"
119
+ write-host $_.Exception.Message
120
+ $retVal = $false
121
+ }
122
+ }
123
+ else {
124
+
125
+ # If module is not imported, not available and not in online gallery then abort
126
+ write-host " Module $ ( $name ) not imported, not available and not in online gallery, exiting."
127
+ EXIT 1
128
+ }
129
+ }
130
+ }
131
+
132
+ return $retVal
133
+ }
134
+
108
135
#
109
136
# Suppress warnings
110
137
#
@@ -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