This repository has been archived by the owner on Apr 4, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
install_modules.ps1
102 lines (93 loc) · 3.51 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
<#
.SYNOPSIS
This script is used in AWS CodeBuild to install the required PowerShell Modules
for the build process.
#>
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$VerbosePreference = 'SilentlyContinue'
# 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 = '4.9.0'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'InvokeBuild'
ModuleVersion = '5.5.6'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'PSScriptAnalyzer'
ModuleVersion = '1.18.2'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'platyPS'
ModuleVersion = '0.12.0'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
$tempPath = [System.IO.Path]::GetTempPath()
if ($PSVersionTable.PSEdition -eq 'Desktop') {
#5.1
$moduleInstallPath = [System.IO.Path]::Combine($env:ProgramFiles, 'WindowsPowerShell', 'Modules')
# Add the AWSPowerShell Module
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'AWSPowerShell'
ModuleVersion = '3.3.428.0'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
}
elseif ($PSVersionTable.PSEdition -eq 'Core') {
$moduleInstallPath = [System.IO.Path]::Combine($env:ProgramFiles, 'PowerShell', 'Modules')
# Add the AWSPowerShell.NetCore Module
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'AWSPowerShell.NetCore'
ModuleVersion = '3.3.428.0'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
}
elseif ($PSVersionTable.Platform -eq 'Unix') {
$moduleInstallPath = [System.IO.Path]::Combine('/', 'usr', 'local', 'share', 'powershell', 'Modules')
# Add the AWSPowerShell.NetCore Module
$null = $modulesToInstall.Add(([PSCustomObject]@{
ModuleName = 'AWSPowerShell.NetCore'
ModuleVersion = '3.3.428.0'
BucketName = 'ps-invoke-modules'
KeyPrefix = ''
}))
}
else{
throw 'Unrecognized OS platform'
}
'Installing PowerShell Modules'
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
$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
}