forked from cisagov/ScubaGear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetUp.ps1
155 lines (132 loc) · 5.9 KB
/
SetUp.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
#Requires -Version 5.1
<#
.SYNOPSIS
This script installs the required Powershell modules used by the
assessment tool
.DESCRIPTION
Installs the modules required to support SCuBAGear. If the Force
switch is set then any existing module will be re-installed even if
already at latest version. If the SkipUpdate switch is set then any
existing module will not be updated to th latest version.
.EXAMPLE
.\Setup.ps1
.NOTES
Executing the script with no switches set will install the latest
version of a module if not already installed.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false, HelpMessage = 'Installs a given module and overrides warning messages about module installation conflicts. If a module with the same name already exists on the computer, Force allows for multiple versions to be installed. If there is an existing module with the same name and version, Force overwrites that version')]
[switch]
$Force,
[Parameter(HelpMessage = 'If specified then modules will not be updated to latest version')]
[switch]
$SkipUpdate,
[Parameter(HelpMessage = 'Do not automatically trust the PSGallery repository for module installation')]
[switch]
$DoNotAutoTrustRepository,
[Parameter(HelpMessage = 'Do not download OPA')]
[switch]
$NoOPA,
[Parameter(Mandatory = $false, HelpMessage = 'The version of OPA Rego to be downloaded, must be in "x.x.x" format')]
[Alias('version')]
[string]
$ExpectedVersion = '0.59.0',
[Parameter(Mandatory = $false, HelpMessage = 'The operating system the program is running on')]
[ValidateSet('Windows','MacOS','Linux')]
[Alias('os')]
[string]
$OperatingSystem = "Windows",
[Parameter(Mandatory = $false, HelpMessage = 'The file name that the opa executable is to be saved as')]
[Alias('name')]
[string]
$OPAExe = "",
[Parameter(Mandatory=$false)]
[ValidateScript({Test-Path -Path $_ -PathType Container})]
[string]
$ScubaParentDirectory = $env:USERPROFILE
)
# Set preferences for writing messages
$DebugPreference = "Continue"
$InformationPreference = "Continue"
if (-not $DoNotAutoTrustRepository) {
$Policy = Get-PSRepository -Name "PSGallery" | Select-Object -Property -InstallationPolicy
if ($($Policy.InstallationPolicy) -ne "Trusted") {
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
Write-Information -MessageData "Setting PSGallery repository to trusted."
}
}
# Start a stopwatch to time module installation elapsed time
$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$RequiredModulesPath = Join-Path -Path $PSScriptRoot -ChildPath "PowerShell\ScubaGear\RequiredVersions.ps1"
if (Test-Path -Path $RequiredModulesPath) {
. $RequiredModulesPath
}
if ($ModuleList) {
# Add PowerShellGet to beginning of ModuleList for installing required modules.
$ModuleList = ,@{
ModuleName = 'PowerShellGet'
ModuleVersion = [version] '2.1.0'
MaximumVersion = [version] '2.99.99999'
} + $ModuleList
}
else {
throw "Required modules list is required."
}
foreach ($Module in $ModuleList) {
$ModuleName = $Module.ModuleName
if (Get-Module -ListAvailable -Name $ModuleName) {
$HighestInstalledVersion = (Get-Module -ListAvailable -Name $ModuleName | Sort-Object Version -Descending | Select-Object Version -First 1).Version
$LatestVersion = [Version](Find-Module -Name $ModuleName -MinimumVersion $Module.ModuleVersion -MaximumVersion $Module.MaximumVersion).Version
if ($HighestInstalledVersion -ge $LatestVersion) {
Write-Debug "${ModuleName}: ${HighestInstalledVersion} already has latest installed."
if ($Force -eq $true) {
Install-Module -Name $ModuleName `
-Force `
-AllowClobber `
-Scope CurrentUser `
-MaximumVersion $Module.MaximumVersion
Write-Information -MessageData "Re-installing module to latest acceptable version: ${ModuleName}."
}
}
else {
if ($SkipUpdate -eq $true) {
Write-Debug "Skipping update for ${ModuleName}: ${HighestInstalledVersion} to newer version ${LatestVersion}."
}
else {
Install-Module -Name $ModuleName `
-Force `
-AllowClobber `
-Scope CurrentUser `
-MaximumVersion $Module.MaximumVersion
$MaxInstalledVersion = (Get-Module -ListAvailable -Name $ModuleName | Sort-Object Version -Descending | Select-Object Version -First 1).Version
Write-Information -MessageData "${ModuleName}: ${HighestInstalledVersion} updated to version ${MaxInstalledVersion}."
}
}
}
else {
Install-Module -Name $ModuleName `
-AllowClobber `
-Scope CurrentUser `
-MaximumVersion $Module.MaximumVersion
$MaxInstalledVersion = (Get-Module -ListAvailable -Name $ModuleName | Sort-Object Version -Descending | Select-Object Version -First 1).Version
Write-Information -MessageData "Installed the latest acceptable version of ${ModuleName}: ${MaxInstalledVersion}."
}
}
if ($NoOPA -eq $true) {
Write-Debug "Skipping Download for OPA.`n"
}
else {
try {
$ScriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
. $ScriptDir\OPA.ps1 -name $OPAExe -version $ExpectedVersion -os $OperatingSystem -ScubaParentDirectory $ScubaParentDirectory
}
catch {
$Error[0] | Format-List -Property * -Force | Out-Host
}
}
# Stop the clock and report total elapsed time
$Stopwatch.stop()
Write-Debug "ScubaGear setup time elapsed: $([math]::Round($stopwatch.Elapsed.TotalSeconds,0)) seconds."
$DebugPreference = "SilentlyContinue"
$InformationPreference = "SilentlyContinue"