forked from techthoughts2/PoshGram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_modules.ps1
143 lines (132 loc) · 6.11 KB
/
install_modules.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
<#
.SYNOPSIS
This script is used in AWS CodeBuild to install the required PowerShell Modules for the build process.
.DESCRIPTION
The version of PowerShell being run will be identified. This may vary depending on what type of build
container you are running and if your buildspec is installing various versions of PowerShell. You will
need to specify each module and version that is required for installation. You also need to specify
which version of that module should be installed. Additionally, you will need to specify the S3 bucket
location where that module currently resides, so that it can be downloaded and installed into the build
container at runtime. This necessitates that you download and upload your required modules to S3 prior to
the build being executed.
.EXAMPLE
Save-Module -Name Pester -RequiredVersion 4.4.5 -Path C:\RequiredModules
Create an S3 bucket in your AWS account
Zip the contents of the Pester Module up (when done properly the .psd1 of the module should be at the root of the zip)
Name the ZIP file Pester_4.4.4 (adjust version as needed) unless you want to modify the logic below
Upload the Pester Zip file up to S3 bucket you just created
.NOTES
Alternatively, you can source PowerShell modules from the PSGallery although this is quite a bit slower
#>
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$VerbosePreference = 'SilentlyContinue'
$galleryDownload = $false # set to false to download from S3
# List of PowerShell Modules required for the build
# The AWS PowerShell Modules are added below, based on the $PSEdition
$modulesToInstall = [System.Collections.ArrayList]::new()
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'Pester'
ModuleVersion = '5.3.3'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'InvokeBuild'
ModuleVersion = '5.9.10'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'PSScriptAnalyzer'
ModuleVersion = '1.20.0'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'platyPS'
ModuleVersion = '0.12.0'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'AWS.Tools.Common'
ModuleVersion = '4.1.98'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'AWS.Tools.SecretsManager'
ModuleVersion = '4.1.98'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$tempPath = [System.IO.Path]::GetTempPath()
if ($PSVersionTable.Platform -eq 'Win32NT') {
$moduleInstallPath = [System.IO.Path]::Combine($env:ProgramFiles, 'WindowsPowerShell', 'Modules')
if ($PSEdition -eq 'Core') {
$moduleInstallPath = [System.IO.Path]::Combine($env:ProgramFiles, 'PowerShell', 'Modules')
}
else {
$moduleInstallPath = [System.IO.Path]::Combine($env:ProgramFiles, 'WindowsPowerShell', 'Modules')
}
}
elseif ($PSVersionTable.Platform -eq 'Unix') {
$moduleInstallPath = [System.IO.Path]::Combine('/', 'usr', 'local', 'share', 'powershell', 'Modules')
}
elseif ($PSEdition -eq 'Desktop') {
$moduleInstallPath = [System.IO.Path]::Combine($env:ProgramFiles, 'WindowsPowerShell', 'Modules')
}
else {
throw 'Unrecognized OS platform'
}
if ($galleryDownload -eq $false) {
'Installing PowerShell Modules from S3'
foreach ($module in $modulesToInstall) {
' - {0} {1}' -f $module.ModuleName, $module.ModuleVersion
# Download file from S3
$key = '{0}_{1}.zip' -f $module.ModuleName, $module.ModuleVersion
$localFile = Join-Path -Path $tempPath -ChildPath $key
# Download modules from S3 to using the AWS CLI
#note: remove --quiet for more verbose output or if S3 download troubleshooting is needed
$s3Uri = 's3://{0}/{1}{2}' -f $module.BucketName, $module.KeyPrefix, $key
& aws s3 cp $s3Uri $localFile --quiet
# Ensure the download worked
if (-not(Test-Path -Path $localFile)) {
$message = 'Failed to download {0}' -f $module.ModuleName
" - $message"
throw $message
}
# Create module path
$modulePath = Join-Path -Path $moduleInstallPath -ChildPath $module.ModuleName
$moduleVersionPath = Join-Path -Path $modulePath -ChildPath $module.ModuleVersion
$null = New-Item -Path $modulePath -ItemType 'Directory' -Force
$null = New-Item -Path $moduleVersionPath -ItemType 'Directory' -Force
# Expand downloaded file
Expand-Archive -Path $localFile -DestinationPath $moduleVersionPath -Force
}
} #if_GalleryDownload
else {
Get-PackageProvider -Name Nuget -ForceBootstrap | Out-Null
'Installing PowerShell Modules from PSGallery'
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
# $NuGetProvider = Get-PackageProvider -Name "NuGet" -ErrorAction SilentlyContinue
# if ( -not $NugetProvider ) {
# Install-PackageProvider -Name "NuGet" -Confirm:$false -Force -Verbose
# }
foreach ($module in $modulesToInstall) {
try {
if ($module.ModuleName -eq 'Pester' -and $PSEdition -eq 'Desktop') {
Install-Module $module.ModuleName -RequiredVersion $module.ModuleVersion -Repository PSGallery -Force -SkipPublisherCheck -ErrorAction Stop
}
else {
Install-Module $module.ModuleName -RequiredVersion $module.ModuleVersion -Repository PSGallery -Confirm:$false -Force -ErrorAction Stop
}
}
catch {
$message = 'Failed to install {0}' -f $module.ModuleName
" - $message"
throw $_
}
}
}