-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTest-PowerShellModuleUpdatesAvailableUsingHashtable.ps1
372 lines (323 loc) · 22.8 KB
/
Test-PowerShellModuleUpdatesAvailableUsingHashtable.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
function Test-PowerShellModuleUpdatesAvailableUsingHashtable {
<#
.SYNOPSIS
Tests to see if updates are available for a PowerShell module based on entries in a
hashtable. If updates are available for a PowerShell module, an error or warning
message may optionally be displayed.
.DESCRIPTION
The Test-PowerShellModuleUpdatesAvailableUsingHashtable function steps through each
entry in the supplied hashtable and, if there are updates available, it optionally
throws an error or warning for each module that has updates available. If all
modules are installed and up to date, the function returns $true; otherwise, if any
module is not installed or not up to date, the function returns $false.
.PARAMETER ReferenceToHashtableOfInstalledModules
Is a reference to a hashtable. The hashtable must have keys that are the names of
PowerShell modules with each key's value populated with arrays of
ModuleInfoGrouping objects (the result of Get-Module).
.PARAMETER ThrowErrorIfModuleNotInstalled
Is a switch parameter. If this parameter is specified, an error is thrown for each
module that is not installed. If this parameter is not specified, no error is
thrown.
.PARAMETER ThrowWarningIfModuleNotInstalled
Is a switch parameter. If this parameter is specified, a warning is thrown for each
module that is not installed. If this parameter is not specified, or if the
ThrowErrorIfModuleNotInstalled parameter was specified, no warning is thrown.
.PARAMETER ThrowErrorIfModuleNotUpToDate
Is a switch parameter. If this parameter is specified, an error is thrown for each
module that is not up to date. If this parameter is not specified, no error is
thrown.
.PARAMETER ThrowWarningIfModuleNotUpToDate
Is a switch parameter. If this parameter is specified, a warning is thrown for each
module that is not up to date. If this parameter is not specified, or if the
ThrowErrorIfModuleNotUpToDate parameter was specified, no warning is thrown.
.PARAMETER ReferenceToHashtableOfCustomNotInstalledMessages
Is a reference to a hashtable. The hashtable must have keys that are custom error
or warning messages (string) to be displayed if one or more modules are not
installed. The value for each key must be an array of PowerShell module names
(strings) relevant to that error or warning message.
If this parameter is not supplied, or if a custom error or warning message is not
supplied in the hashtable for a given module, the script will default to using the
following message:
<MODULENAME> module not found. Please install it and then try again.
You can install the <MODULENAME> PowerShell module from the PowerShell Gallery by
running the following command:
Install-Module <MODULENAME>;
If the installation command fails, you may need to upgrade the version of
PowerShellGet. To do so, run the following commands, then restart PowerShell:
Set-ExecutionPolicy Bypass -Scope Process -Force;
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force;
Install-Module PowerShellGet -MinimumVersion 2.2.4 -SkipPublisherCheck -Force -AllowClobber;
.PARAMETER ReferenceToHashtableOfCustomNotUpToDateMessages
Is a reference to a hashtable. The hashtable must have keys that are custom error
or warning messages (string) to be displayed if one or more modules are not
up to date. The value for each key must be an array of PowerShell module names
(strings) relevant to that error or warning message.
If this parameter is not supplied, or if a custom error or warning message is not
supplied in the hashtable for a given module, the script will default to using the
following message:
A newer version of the <MODULENAME> PowerShell module is available. Please consider
updating it by running the following command:
Install-Module <MODULENAME> -Force;
If the installation command fails, you may need to upgrade the version of
PowerShellGet. To do so, run the following commands, then restart PowerShell:
Set-ExecutionPolicy Bypass -Scope Process -Force;
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force;
Install-Module PowerShellGet -MinimumVersion 2.2.4 -SkipPublisherCheck -Force -AllowClobber;
.PARAMETER ReferenceToArrayOfMissingModules
Is a reference to an array. The array must be initialized to be empty. If any
modules are not installed, the names of those modules are added to the array.
.PARAMETER ReferenceToArrayOfOutOfDateModules
Is a reference to an array. The array must be initialized to be empty. If any
modules are not up to date, the names of those modules are added to the array.
.EXAMPLE
$hashtableModuleNameToInstalledModules = @{}
$hashtableModuleNameToInstalledModules.Add('PnP.PowerShell', @())
$hashtableModuleNameToInstalledModules.Add('Microsoft.Graph.Authentication', @())
$hashtableModuleNameToInstalledModules.Add('Microsoft.Graph.Groups', @())
$hashtableModuleNameToInstalledModules.Add('Microsoft.Graph.Users', @())
$refHashtableModuleNameToInstalledModules = [ref]$hashtableModuleNameToInstalledModules
Get-PowerShellModuleUsingHashtable -ReferenceToHashtable $refHashtableModuleNameToInstalledModules
$hashtableCustomNotInstalledMessageToModuleNames = @{}
$strGraphNotInstalledMessage = 'Microsoft.Graph.Authentication, Microsoft.Graph.Groups, and/or Microsoft.Graph.Users modules were not found. Please install the full Microsoft.Graph module and then try again.' + [System.Environment]::NewLine + 'You can install the Microsoft.Graph PowerShell module from the PowerShell Gallery by running the following command:' + [System.Environment]::NewLine + 'Install-Module Microsoft.Graph;' + [System.Environment]::NewLine + [System.Environment]::NewLine + 'If the installation command fails, you may need to upgrade the version of PowerShellGet. To do so, run the following commands, then restart PowerShell:' + [System.Environment]::NewLine + 'Set-ExecutionPolicy Bypass -Scope Process -Force;' + [System.Environment]::NewLine + '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;' + [System.Environment]::NewLine + 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force;' + [System.Environment]::NewLine + 'Install-Module PowerShellGet -MinimumVersion 2.2.4 -SkipPublisherCheck -Force -AllowClobber;' + [System.Environment]::NewLine + [System.Environment]::NewLine
$hashtableCustomNotInstalledMessageToModuleNames.Add($strGraphNotInstalledMessage, @('Microsoft.Graph.Authentication', 'Microsoft.Graph.Groups', 'Microsoft.Graph.Users'))
$refhashtableCustomNotInstalledMessageToModuleNames = [ref]$hashtableCustomNotInstalledMessageToModuleNames
$hashtableCustomNotUpToDateMessageToModuleNames = @{}
$strGraphNotUpToDateMessage = 'A newer version of the Microsoft.Graph.Authentication, Microsoft.Graph.Groups, and/or Microsoft.Graph.Users modules was found. Please consider updating it by running the following command:' + [System.Environment]::NewLine + 'Install-Module Microsoft.Graph -Force;' + [System.Environment]::NewLine + [System.Environment]::NewLine + 'If the installation command fails, you may need to upgrade the version of PowerShellGet. To do so, run the following commands, then restart PowerShell:' + [System.Environment]::NewLine + 'Set-ExecutionPolicy Bypass -Scope Process -Force;' + [System.Environment]::NewLine + '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;' + [System.Environment]::NewLine + 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force;' + [System.Environment]::NewLine + 'Install-Module PowerShellGet -MinimumVersion 2.2.4 -SkipPublisherCheck -Force -AllowClobber;' + [System.Environment]::NewLine + [System.Environment]::NewLine
$hashtableCustomNotUpToDateMessageToModuleNames.Add($strGraphNotUpToDateMessage, @('Microsoft.Graph.Authentication', 'Microsoft.Graph.Groups', 'Microsoft.Graph.Users'))
$refhashtableCustomNotUpToDateMessageToModuleNames = [ref]$hashtableCustomNotUpToDateMessageToModuleNames
$boolResult = Test-PowerShellModuleUpdatesAvailableUsingHashtable -ReferenceToHashtableOfInstalledModules $refHashtableModuleNameToInstalledModules -ThrowErrorIfModuleNotInstalled -ThrowWarningIfModuleNotUpToDate -ReferenceToHashtableOfCustomNotInstalledMessages $refhashtableCustomNotInstalledMessageToModuleNames -ReferenceToHashtableOfCustomNotUpToDateMessages $refhashtableCustomNotUpToDateMessageToModuleNames
This example checks to see if the PnP.PowerShell, Microsoft.Graph.Authentication,
Microsoft.Graph.Groups, and Microsoft.Graph.Users modules are installed. If any of
these modules are not installed, an error is thrown for the PnP.PowerShell module
or the group of Microsoft.Graph modules, respectively, and $boolResult is set to
$false. If any of these modules are installed but not up to date, a warning
message is thrown for the PnP.PowerShell module or the group of Microsoft.Graph
modules, respectively, and $boolResult is set to false. If all modules are
installed and up to date, $boolResult is set to $true.
.OUTPUTS
[boolean] - Returns $true if all modules are installed and up to date; otherwise,
returns $false.
.NOTES
Requires PowerShell v5.0 or newer
#>
#region License ################################################################
# Copyright (c) 2025 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion License ################################################################
#region DownloadLocationNotice ################################################
# The most up-to-date version of this script can be found on the author's GitHub
# repository at https://github.com/franklesniak/PowerShell_Resources
#endregion DownloadLocationNotice ################################################
# Version 1.1.20250106.0
[CmdletBinding()]
[OutputType([Boolean])]
param (
[Parameter(Mandatory = $true)][ref]$ReferenceToHashtableOfInstalledModules,
[switch]$ThrowErrorIfModuleNotInstalled,
[switch]$ThrowWarningIfModuleNotInstalled,
[switch]$ThrowErrorIfModuleNotUpToDate,
[switch]$ThrowWarningIfModuleNotUpToDate,
[Parameter(Mandatory = $false)][ref]$ReferenceToHashtableOfCustomNotInstalledMessages,
[Parameter(Mandatory = $false)][ref]$ReferenceToHashtableOfCustomNotUpToDateMessages,
[Parameter(Mandatory = $false)][ref]$ReferenceToArrayOfMissingModules,
[Parameter(Mandatory = $false)][ref]$ReferenceToArrayOfOutdatedModules
)
function Get-PSVersion {
# .SYNOPSIS
# Returns the version of PowerShell that is running.
#
# .DESCRIPTION
# The function outputs a [version] object representing the version of
# PowerShell that is running.
#
# On versions of PowerShell greater than or equal to version 2.0, this
# function returns the equivalent of $PSVersionTable.PSVersion
#
# PowerShell 1.0 does not have a $PSVersionTable variable, so this
# function returns [version]('1.0') on PowerShell 1.0.
#
# .EXAMPLE
# $versionPS = Get-PSVersion
# # $versionPS now contains the version of PowerShell that is running.
# # On versions of PowerShell greater than or equal to version 2.0,
# # this function returns the equivalent of $PSVersionTable.PSVersion.
#
# .INPUTS
# None. You can't pipe objects to Get-PSVersion.
#
# .OUTPUTS
# System.Version. Get-PSVersion returns a [version] value indiciating
# the version of PowerShell that is running.
#
# .NOTES
# Version: 1.0.20250106.0
#region License ####################################################
# Copyright (c) 2025 Frank Lesniak
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#endregion License ####################################################
if (Test-Path variable:\PSVersionTable) {
return ($PSVersionTable.PSVersion)
} else {
return ([version]('1.0'))
}
}
$versionPS = Get-PSVersion
if ($versionPS -lt ([version]'5.0')) {
Write-Warning 'Test-PowerShellModuleUpdatesAvailableUsingHashtable requires PowerShell version 5.0 or newer.'
return $false
}
$boolThrowErrorForMissingModule = $false
$boolThrowWarningForMissingModule = $false
if ($ThrowErrorIfModuleNotInstalled.IsPresent -eq $true) {
$boolThrowErrorForMissingModule = $true
} elseif ($ThrowWarningIfModuleNotInstalled.IsPresent -eq $true) {
$boolThrowWarningForMissingModule = $true
}
$boolThrowErrorForOutdatedModule = $false
$boolThrowWarningForOutdatedModule = $false
if ($ThrowErrorIfModuleNotUpToDate.IsPresent -eq $true) {
$boolThrowErrorForOutdatedModule = $true
} elseif ($ThrowWarningIfModuleNotUpToDate.IsPresent -eq $true) {
$boolThrowWarningForOutdatedModule = $true
}
$VerbosePreferenceAtStartOfFunction = $VerbosePreference
$boolResult = $true
$hashtableMessagesToThrowForMissingModule = @{}
$hashtableModuleNameToCustomMessageToThrowForMissingModule = @{}
if ($null -ne $ReferenceToHashtableOfCustomNotInstalledMessages) {
$arrMessages = @(($ReferenceToHashtableOfCustomNotInstalledMessages.Value).Keys)
foreach ($strMessage in $arrMessages) {
$hashtableMessagesToThrowForMissingModule.Add($strMessage, $false)
($ReferenceToHashtableOfCustomNotInstalledMessages.Value).Item($strMessage) | ForEach-Object {
$hashtableModuleNameToCustomMessageToThrowForMissingModule.Add($_, $strMessage)
}
}
}
$hashtableMessagesToThrowForOutdatedModule = @{}
$hashtableModuleNameToCustomMessageToThrowForOutdatedModule = @{}
if ($null -ne $ReferenceToHashtableOfCustomNotUpToDateMessages) {
$arrMessages = @(($ReferenceToHashtableOfCustomNotUpToDateMessages.Value).Keys)
foreach ($strMessage in $arrMessages) {
$hashtableMessagesToThrowForOutdatedModule.Add($strMessage, $false)
($ReferenceToHashtableOfCustomNotUpToDateMessages.Value).Item($strMessage) | ForEach-Object {
$hashtableModuleNameToCustomMessageToThrowForOutdatedModule.Add($_, $strMessage)
}
}
}
$arrModuleNames = @(($ReferenceToHashtableOfInstalledModules.Value).Keys)
foreach ($strModuleName in $arrModuleNames) {
$arrInstalledModules = @(($ReferenceToHashtableOfInstalledModules.Value).Item($strModuleName))
if ($arrInstalledModules.Count -eq 0) {
$boolResult = $false
if ($hashtableModuleNameToCustomMessageToThrowForMissingModule.ContainsKey($strModuleName) -eq $true) {
$strMessage = $hashtableModuleNameToCustomMessageToThrowForMissingModule.Item($strModuleName)
$hashtableMessagesToThrowForMissingModule.Item($strMessage) = $true
} else {
$strMessage = $strModuleName + ' module not found. Please install it and then try again.' + [System.Environment]::NewLine + 'You can install the ' + $strModuleName + ' PowerShell module from the PowerShell Gallery by running the following command:' + [System.Environment]::NewLine + 'Install-Module ' + $strModuleName + ';' + [System.Environment]::NewLine + [System.Environment]::NewLine + 'If the installation command fails, you may need to upgrade the version of PowerShellGet. To do so, run the following commands, then restart PowerShell:' + [System.Environment]::NewLine + 'Set-ExecutionPolicy Bypass -Scope Process -Force;' + [System.Environment]::NewLine + '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;' + [System.Environment]::NewLine + 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force;' + [System.Environment]::NewLine + 'Install-Module PowerShellGet -MinimumVersion 2.2.4 -SkipPublisherCheck -Force -AllowClobber;' + [System.Environment]::NewLine + [System.Environment]::NewLine
$hashtableMessagesToThrowForMissingModule.Add($strMessage, $true)
}
if ($null -ne $ReferenceToArrayOfMissingModules) {
($ReferenceToArrayOfMissingModules.Value) += $strModuleName
}
} else {
$versionNewestInstalledModule = ($arrInstalledModules | ForEach-Object { [version]($_.Version) } | Sort-Object)[-1]
$arrModuleNewestInstalledModule = @($arrInstalledModules | Where-Object { ([version]($_.Version)) -eq $versionNewestInstalledModule })
# In the event there are multiple installations of the same version, reduce to a
# single instance of the module
if ($arrModuleNewestInstalledModule.Count -gt 1) {
$moduleNewestInstalled = @($arrModuleNewestInstalledModule | Select-Object -Unique)[0]
} else {
$moduleNewestInstalled = $arrModuleNewestInstalledModule[0]
}
$VerbosePreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
$moduleNewestAvailable = Find-Module -Name $strModuleName -ErrorAction SilentlyContinue
$VerbosePreference = $VerbosePreferenceAtStartOfFunction
if ($null -ne $moduleNewestAvailable) {
if ($moduleNewestAvailable.Version -gt $moduleNewestInstalled.Version) {
# A newer version is available
$boolResult = $false
if ($hashtableModuleNameToCustomMessageToThrowForOutdatedModule.ContainsKey($strModuleName) -eq $true) {
$strMessage = $hashtableModuleNameToCustomMessageToThrowForOutdatedModule.Item($strModuleName)
$hashtableMessagesToThrowForOutdatedModule.Item($strMessage) = $true
} else {
$strMessage = 'A newer version of the ' + $strModuleName + ' PowerShell module is available. Please consider updating it by running the following command:' + [System.Environment]::NewLine + 'Install-Module ' + $strModuleName + ' -Force;' + [System.Environment]::NewLine + [System.Environment]::NewLine + 'If the installation command fails, you may need to upgrade the version of PowerShellGet. To do so, run the following commands, then restart PowerShell:' + [System.Environment]::NewLine + 'Set-ExecutionPolicy Bypass -Scope Process -Force;' + [System.Environment]::NewLine + '[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;' + [System.Environment]::NewLine + 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force;' + [System.Environment]::NewLine + 'Install-Module PowerShellGet -MinimumVersion 2.2.4 -SkipPublisherCheck -Force -AllowClobber;' + [System.Environment]::NewLine + [System.Environment]::NewLine
$hashtableMessagesToThrowForOutdatedModule.Add($strMessage, $true)
}
if ($null -ne $ReferenceToArrayOfOutdatedModules) {
($ReferenceToArrayOfOutdatedModules.Value) += $strModuleName
}
}
} else {
# Couldn't find the module in the PowerShell Gallery
}
}
}
if ($boolThrowErrorForMissingModule -eq $true) {
$arrMessages = @($hashtableMessagesToThrowForMissingModule.Keys)
foreach ($strMessage in $arrMessages) {
if ($hashtableMessagesToThrowForMissingModule.Item($strMessage) -eq $true) {
Write-Error $strMessage
}
}
} elseif ($boolThrowWarningForMissingModule -eq $true) {
$arrMessages = @($hashtableMessagesToThrowForMissingModule.Keys)
foreach ($strMessage in $arrMessages) {
if ($hashtableMessagesToThrowForMissingModule.Item($strMessage) -eq $true) {
Write-Warning $strMessage
}
}
}
if ($boolThrowErrorForOutdatedModule -eq $true) {
$arrMessages = @($hashtableMessagesToThrowForOutdatedModule.Keys)
foreach ($strMessage in $arrMessages) {
if ($hashtableMessagesToThrowForOutdatedModule.Item($strMessage) -eq $true) {
Write-Error $strMessage
}
}
} elseif ($boolThrowWarningForOutdatedModule -eq $true) {
$arrMessages = @($hashtableMessagesToThrowForOutdatedModule.Keys)
foreach ($strMessage in $arrMessages) {
if ($hashtableMessagesToThrowForOutdatedModule.Item($strMessage) -eq $true) {
Write-Warning $strMessage
}
}
}
return $boolResult
}