-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTest-PowerShellModuleInstalledUsingHashtable.ps1
179 lines (153 loc) · 11.3 KB
/
Test-PowerShellModuleInstalledUsingHashtable.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
function Test-PowerShellModuleInstalledUsingHashtable {
<#
.SYNOPSIS
Tests to see if a PowerShell module is installed based on entries in a hashtable.
If the PowerShell module is not installed, an error or warning message may
optionally be displayed.
.DESCRIPTION
The Test-PowerShellModuleInstalledUsingHashtable function steps through each entry
in the supplied hashtable and, if there are any modules not installed, it
optionally throws an error or warning for each module that is not installed. If all
modules are installed, the function returns $true; otherwise, if any module is not
installed, 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 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 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.
.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
$boolResult = Test-PowerShellModuleInstalledUsingHashtable -ReferenceToHashtableOfInstalledModules $refHashtableModuleNameToInstalledModules -ThrowErrorIfModuleNotInstalled -ReferenceToHashtableOfCustomNotInstalledMessages $refhashtableCustomNotInstalledMessageToModuleNames
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 all modules are installed, $boolResult is set to $true.
.OUTPUTS
[boolean] - Returns $true if all modules are installed; otherwise, returns $false.
#>
#region License ################################################################
# Copyright (c) 2024 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.20240401.0
[CmdletBinding()]
[OutputType([Boolean])]
param (
[Parameter(Mandatory = $true)][ref]$ReferenceToHashtableOfInstalledModules,
[switch]$ThrowErrorIfModuleNotInstalled,
[switch]$ThrowWarningIfModuleNotInstalled,
[Parameter(Mandatory = $false)][ref]$ReferenceToHashtableOfCustomNotInstalledMessages,
[Parameter(Mandatory = $false)][ref]$ReferenceToArrayOfMissingModules
)
$boolThrowErrorForMissingModule = $false
$boolThrowWarningForMissingModule = $false
if ($ThrowErrorIfModuleNotInstalled.IsPresent -eq $true) {
$boolThrowErrorForMissingModule = $true
} elseif ($ThrowWarningIfModuleNotInstalled.IsPresent -eq $true) {
$boolThrowWarningForMissingModule = $true
}
$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)
}
}
}
$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
}
}
}
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
}
}
}
return $boolResult
}