-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall-modules.ps1
More file actions
317 lines (264 loc) · 11 KB
/
Copy pathinstall-modules.ps1
File metadata and controls
317 lines (264 loc) · 11 KB
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
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Install PowerShell modules with version constraint support.
.DESCRIPTION
Reusable script for installing PowerShell modules. Supports version constraints
and can be used in Docker builds, local development, or any PowerShell environment.
Designed to work with Hermes's PowerShell best practices:
- Proper error handling with ErrorAction Stop
- Stream-based output (Write-Host for info, Write-Error for errors)
- Exit codes for CI/CD integration
.PARAMETER Modules
Space or comma-separated list of module names with optional version constraints.
Version syntax:
- ModuleName - Latest version
- ModuleName@1.2.3 - Exact version
- ModuleName@>=1.0.0 - Minimum version
- ModuleName@<=2.0.0 - Maximum version
.PARAMETER Scope
Installation scope: 'AllUsers' or 'CurrentUser'. Default: AllUsers
.PARAMETER SkipPublisherCheck
Skip publisher validation during installation. Default: true
.PARAMETER Force
Force installation even if module already exists. Default: false
.EXAMPLE
./install-modules.ps1 -Modules "Pester PSScriptAnalyzer"
.EXAMPLE
./install-modules.ps1 -Modules "Az.Accounts@>=2.0.0,Pester@5.5.0" -Scope CurrentUser
.EXAMPLE
# From environment variable (Docker-friendly)
$env:INSTALL_PS_MODULES = "Pester PSScriptAnalyzer"
./install-modules.ps1
#>
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[string]$Modules = $env:INSTALL_PS_MODULES,
[Parameter()]
[ValidateSet('AllUsers', 'CurrentUser')]
[string]$Scope = $env:MODULE_INSTALL_SCOPE ?? 'AllUsers',
[Parameter()]
[bool]$SkipPublisherCheck = [System.Convert]::ToBoolean($env:SKIP_PUBLISHER_CHECK ?? 'true'),
[Parameter()]
[switch]$Force
)
# Enable strict error handling
$ErrorActionPreference = 'Stop'
# Track installation stats
$script:SuccessCount = 0
$script:FailureCount = 0
$script:SkippedCount = 0
function Write-StatusMessage {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Message,
[Parameter()]
[ValidateSet('Info', 'Success', 'Warning', 'Error')]
[string]$Level = 'Info'
)
$prefix = switch ($Level) {
'Info' { '📦' }
'Success' { '✓' }
'Warning' { '⚠️' }
'Error' { '❌' }
}
$color = switch ($Level) {
'Info' { 'Cyan' }
'Success' { 'Green' }
'Warning' { 'Yellow' }
'Error' { 'Red' }
}
Write-Host "$prefix $Message" -ForegroundColor $color
}
function Initialize-PSRepository {
[CmdletBinding()]
param()
Write-StatusMessage -Message "Initializing PSGallery repository..." -Level Info
try {
# Check if PSGallery exists
$gallery = Get-PSRepository -Name PSGallery -ErrorAction SilentlyContinue
if (-not $gallery) {
Write-StatusMessage -Message "PSGallery not found, registering..." -Level Warning
Register-PSRepository -Default -ErrorAction SilentlyContinue
}
# Set as trusted
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction Stop
Write-StatusMessage -Message "PSGallery is trusted and ready" -Level Success
}
catch {
Write-Error "Failed to initialize PSGallery: $_"
exit 1
}
}
function Install-ModuleWithVersion {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$ModuleSpec,
[Parameter(Mandatory)]
[string]$InstallScope,
[Parameter()]
[bool]$SkipCheck,
[Parameter()]
[bool]$ForceInstall
)
# Parse module name and version constraint
if ($ModuleSpec -match '^([^@]+)@(.+)$') {
$moduleName = $matches[1]
$versionSpec = $matches[2]
}
else {
$moduleName = $ModuleSpec
$versionSpec = $null
}
Write-StatusMessage -Message "Processing module: $moduleName" -Level Info
# Check if module already exists (unless Force is specified)
if (-not $ForceInstall) {
$existingModule = Get-Module -ListAvailable -Name $moduleName -ErrorAction SilentlyContinue |
Select-Object -First 1
if ($existingModule) {
Write-StatusMessage -Message "Module $moduleName (v$($existingModule.Version)) already installed, skipping" -Level Info
$script:SkippedCount++
return
}
}
try {
# Build Install-Module parameters
$installParams = @{
Name = $moduleName
Scope = $InstallScope
Force = $true
SkipPublisherCheck = $SkipCheck
ErrorAction = 'Stop'
}
# Add version constraint if specified
if ($versionSpec) {
Write-StatusMessage -Message "Installing $moduleName with version constraint: $versionSpec" -Level Info
if ($versionSpec -match '^>=(.+)$') {
$minVersion = $matches[1]
$installParams['MinimumVersion'] = $minVersion
}
elseif ($versionSpec -match '^<=(.+)$') {
$maxVersion = $matches[1]
$installParams['MaximumVersion'] = $maxVersion
}
elseif ($versionSpec -match '^>(.+)$') {
# Greater than - use minimum version and filter manually
$minVersion = $matches[1]
Write-StatusMessage -Message "Version constraint >$minVersion not directly supported, using >=$minVersion" -Level Warning
$installParams['MinimumVersion'] = $minVersion
}
elseif ($versionSpec -match '^<(.+)$') {
# Less than - use maximum version
$maxVersion = $matches[1]
$installParams['MaximumVersion'] = $maxVersion
}
else {
# Exact version
$installParams['RequiredVersion'] = $versionSpec
}
}
else {
Write-StatusMessage -Message "Installing $moduleName (latest version)" -Level Info
}
# Install the module
Install-Module @installParams
# Verify installation
$installed = Get-Module -ListAvailable -Name $moduleName -ErrorAction SilentlyContinue |
Select-Object -First 1
if ($installed) {
Write-StatusMessage -Message "Successfully installed: $moduleName v$($installed.Version)" -Level Success
$script:SuccessCount++
}
else {
throw "Module installation completed but module not found"
}
}
catch {
Write-StatusMessage -Message "Failed to install module $moduleName" -Level Error
Write-Error ("Module installation failed for {0} - {1}" -f $moduleName, $_.Exception.Message)
$script:FailureCount++
# Exit immediately on failure for fail-fast behavior
exit 1
}
}
function Show-InstallationSummary {
[CmdletBinding()]
param()
Write-Host ""
Write-Host "═══════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host " PowerShell Module Installation Summary" -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""
Write-Host " ✓ Successful: $script:SuccessCount" -ForegroundColor Green
Write-Host " ⊘ Skipped: $script:SkippedCount" -ForegroundColor Yellow
Write-Host " ❌ Failed: $script:FailureCount" -ForegroundColor Red
Write-Host ""
if ($script:SuccessCount -gt 0) {
Write-Host "Installed modules:" -ForegroundColor Cyan
Get-Module -ListAvailable |
Sort-Object Name, Version -Descending |
Group-Object Name |
ForEach-Object { $_.Group | Select-Object -First 1 } |
Format-Table Name, Version, Path -AutoSize |
Out-String |
Write-Host
}
Write-Host "═══════════════════════════════════════════════════════" -ForegroundColor Cyan
}
# Main execution
try {
Write-Host ""
Write-Host "═══════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host " PowerShell Module Installation Script" -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""
# Validate input
if ([string]::IsNullOrWhiteSpace($Modules)) {
Write-StatusMessage -Message "No modules specified. Set -Modules parameter or INSTALL_PS_MODULES environment variable." -Level Warning
Write-Host ""
Write-Host "Usage examples:" -ForegroundColor Yellow
Write-Host " ./install-modules.ps1 -Modules 'Pester PSScriptAnalyzer'" -ForegroundColor White
Write-Host " ./install-modules.ps1 -Modules 'Az.Accounts@>=2.0.0,Pester@5.5.0'" -ForegroundColor White
Write-Host " env INSTALL_PS_MODULES='Pester' ./install-modules.ps1" -ForegroundColor White
Write-Host ""
exit 0
}
Write-StatusMessage -Message "Configuration:" -Level Info
Write-Host " Modules: $Modules" -ForegroundColor White
Write-Host " Scope: $Scope" -ForegroundColor White
Write-Host " Skip Publisher Check: $SkipPublisherCheck" -ForegroundColor White
Write-Host " Force Reinstall: $Force" -ForegroundColor White
Write-Host ""
# Initialize PSGallery
Initialize-PSRepository
Write-Host ""
# Parse module list (support both space and comma separation)
$moduleList = $Modules -replace '[,;]', ' ' -split '\s+' | Where-Object { $_ }
Write-StatusMessage -Message "Found $($moduleList.Count) module(s) to process" -Level Info
Write-Host ""
# Install each module
foreach ($moduleSpec in $moduleList) {
Install-ModuleWithVersion -ModuleSpec $moduleSpec `
-InstallScope $Scope `
-SkipCheck $SkipPublisherCheck `
-ForceInstall $Force
Write-Host ""
}
# Show summary
Show-InstallationSummary
# Exit with appropriate code
if ($script:FailureCount -gt 0) {
exit 1
}
else {
exit 0
}
}
catch {
Write-StatusMessage -Message "Unexpected error occurred" -Level Error
Write-Error $_.Exception.Message
exit 1
}