Skip to content

Commit 6d472b8

Browse files
authored
Merge pull request #1190 from anosov1960/master
Optimized LoadModule
2 parents f1425bb + 7eaf3bd commit 6d472b8

File tree

1 file changed

+55
-28
lines changed

1 file changed

+55
-28
lines changed

samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,6 @@ param (
3939
[switch] $Force
4040
)
4141

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-
6942
function ConvertTo-Hashtable {
7043
[CmdletBinding()]
7144
[OutputType('hashtable')]
@@ -105,6 +78,60 @@ function ConvertTo-Hashtable {
10578
}
10679
}
10780

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+
108135
#
109136
# Suppress warnings
110137
#
@@ -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

Comments
 (0)