Resources/AWS/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
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
<#
.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 neccessitates 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
    AWSPowerShell / AWSPowerShell.NetCore module should be included in all CodeBuild projects and is included below
    Pester, InvokeBuild, PSScriptAnalyzer, platyPS will typically be required by all module builds
    which is why they are included in this build script. Adjust versions as needed.
#>



<%
If ($PLASTER_PARAM_S3Bucket -eq 'PSGallery') {
    @'
$galleryDownload = $true
'@

}
else {
    @'
$galleryDownload = $false
'@

}
%>

$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    = <%=$PLASTER_PARAM_S3Bucket%>
            KeyPrefix     = ''
        }))
$null = $modulesToInstall.Add(([PSCustomObject]@{
            ModuleName    = 'InvokeBuild'
            ModuleVersion = '5.5.6'
            BucketName    = <%=$PLASTER_PARAM_S3Bucket%>
            KeyPrefix     = ''
        }))
$null = $modulesToInstall.Add(([PSCustomObject]@{
            ModuleName    = 'PSScriptAnalyzer'
            ModuleVersion = '1.18.3'
            BucketName    = <%=$PLASTER_PARAM_S3Bucket%>
            KeyPrefix     = ''
        }))
$null = $modulesToInstall.Add(([PSCustomObject]@{
            ModuleName    = 'platyPS'
            ModuleVersion = '0.12.0'
            BucketName    = <%=$PLASTER_PARAM_S3Bucket%>
            KeyPrefix     = ''
        }))

#install the correct version of AWSPowerShell module
# $tempPath = [System.IO.Path]::GetTempPath()
# if ($PSVersionTable.Platform -eq 'Win32NT') {
# #Core-Windows
# $moduleInstallPath = [System.IO.Path]::Combine($env:ProgramFiles, 'WindowsPowerShell', 'Modules')
# if ($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.498.0'
# BucketName = 'ps-invoke-modules'
# KeyPrefix = ''
# }))
# }
# else {
# $moduleInstallPath = [System.IO.Path]::Combine($env:ProgramFiles, 'WindowsPowerShell', 'Modules')
# # Add the AWSPowerShell Module
# $null = $modulesToInstall.Add(([PSCustomObject]@{
# ModuleName = 'AWSPowerShell'
# ModuleVersion = '3.3.498.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.498.0'
# BucketName = 'ps-invoke-modules'
# KeyPrefix = ''
# }))
# }
# elseif ($PSEdition -eq 'Desktop') {
# $moduleInstallPath = [System.IO.Path]::Combine($env:ProgramFiles, 'WindowsPowerShell', 'Modules')
# # Add the AWSPowerShell Module
# $null = $modulesToInstall.Add(([PSCustomObject]@{
# ModuleName = 'AWSPowerShell'
# ModuleVersion = '3.3.498.0'
# BucketName = 'ps-invoke-modules'
# KeyPrefix = ''
# }))
# }
# else {
# throw 'Unrecognized OS platform'
# }

if ($galleryDownload -eq $false) {
    '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
        #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'
    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 $_
        }
    }
}