Skip to content

Commit 3646253

Browse files
authored
Update windows-provisioner.ps1 for 2022
1 parent c1b536a commit 3646253

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed
Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,74 @@
1+
# Set up preferences for output
12
$ErrorActionPreference = "Continue"
23
$VerbosePreference = "Continue"
4+
$ProgressPreference = "SilentlyContinue"
35

4-
# Install Chocolatey
5-
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
6-
$env:chocolateyUseWindowsCompression = 'true'
7-
Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression
8-
9-
# Add Chocolatey to powershell profile
10-
$ChocoProfileValue = @'
11-
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
12-
if (Test-Path($ChocolateyProfile)) {
13-
Import-Module "$ChocolateyProfile"
6+
# Print out our powershell version just so we know what the default is.
7+
$PSVersionTable
8+
9+
# Just checking disk space
10+
Get-PSDrive C | Select-Object Used,Free
11+
12+
# Make sure that we have a profile to set and write to. If not, create it.
13+
$powershellProfile = "C:\Users\Administrator\Documents\WindowsPowerShell\profile.ps1"
14+
Write-Output "Checking for profile at $powershellProfile"
15+
if (!(Test-Path -Path $powershellProfile)) {
16+
Write-Output "Creating $powershellProfile cause it doesn't exist yet"
17+
New-Item -Type File -Path $powershellProfile -Force
18+
}
19+
Write-Output "Checking for profile at $profile"
20+
if (!(Test-Path -Path $profile)) {
21+
Write-Output "Creating $profile cause it doesn't exist yet"
22+
New-Item -Type File -Path $profile -Force
1423
}
1524

16-
refreshenv
17-
'@
18-
# Write it to the $profile location
19-
Set-Content -Path "$PsHome\Microsoft.PowerShell_profile.ps1" -Value $ChocoProfileValue -Force
20-
# Source it
21-
. "$PsHome\Microsoft.PowerShell_profile.ps1"
25+
# Install Chocolatey
26+
Write-Output "Setting up Chocolatey install execution policy"
27+
Get-ExecutionPolicy
28+
Set-ExecutionPolicy Bypass -Scope Process -Force
29+
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
30+
Write-Output "Downloading and running Chocolatey install script"
31+
# NOTE : We use the System.Net.WebClient object, because using Invoke-Webrequest is ridiculously slow with larger files. Using this halves download times.
32+
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
33+
choco feature enable -n allowGlobalConfirmation
2234

23-
refreshenv
35+
# NOTE / IMPORTANT : DO NOT SET THE POWERSHELL PROFILE. FOR SOME REASON IT BREAKS THE SFTP PROVISIONER.
36+
# Likely due to the fact that the powershell profile will generate output when it is sourced, which causes
37+
# a "Error processing command: Error uploading ps script containing env vars: sftpSession error: packet too long"
38+
# failure when trying to upload the next script.
2439

25-
Write-Host "Installing cloudwatch agent..."
26-
Invoke-WebRequest -Uri https://s3.amazonaws.com/amazoncloudwatch-agent/windows/amd64/latest/amazon-cloudwatch-agent.msi -OutFile C:\amazon-cloudwatch-agent.msi
40+
# Install cloudwatch agent
41+
Write-Output "Installing cloudwatch agent..."
42+
$cloudwatchURL = "https://s3.amazonaws.com/amazoncloudwatch-agent/windows/amd64/latest/amazon-cloudwatch-agent.msi"
43+
$cloudwatchLocation = "C:\amazon-cloudwatch-agent.msi"
2744
$cloudwatchParams = '/i', 'C:\amazon-cloudwatch-agent.msi', '/qn', '/L*v', 'C:\CloudwatchInstall.log'
28-
Start-Process "msiexec.exe" $cloudwatchParams -Wait -NoNewWindow
29-
Remove-Item C:\amazon-cloudwatch-agent.msi
45+
$webClient = New-Object System.Net.WebClient
46+
$webClient.Downloadfile($cloudwatchURL, $cloudwatchLocation)
47+
Write-Output "Installing Cloudwatch agent from $cloudwatchLocation"
48+
Start-Process "msiexec.exe" -ArgumentList $cloudwatchParams -Wait -NoNewWindow
49+
Remove-Item $cloudwatchLocation
3050

3151
# Install dependent tools
32-
Write-Host "Installing additional development tools"
52+
Write-Output "Installing additional development tools"
3353
choco install git awscli -y
3454
refreshenv
3555

36-
Write-Host "Creating actions-runner directory for the GH Action installtion"
56+
# Install Github Actions runner
57+
Write-Output "Creating actions-runner directory for the GH Action installtion"
3758
New-Item -ItemType Directory -Path C:\actions-runner ; Set-Location C:\actions-runner
38-
39-
Write-Host "Downloading the GH Action runner from ${action_runner_url}"
40-
Invoke-WebRequest -Uri ${action_runner_url} -OutFile actions-runner.zip
41-
42-
Write-Host "Un-zip action runner"
59+
Write-Output "Downloading the GH Action runner from ${action_runner_url}"
60+
# Invoke-WebRequest -Uri ${action_runner_url} -OutFile actions-runner.zip
61+
$webClient.Downloadfile("${action_runner_url}", "$PWD\actions-runner.zip")
62+
Write-Output "Unzip action runner"
4363
Expand-Archive -Path actions-runner.zip -DestinationPath .
44-
45-
Write-Host "Delete zip file"
64+
Write-Output "Delete zip file"
4665
Remove-Item actions-runner.zip
4766

4867
$action = New-ScheduledTaskAction -WorkingDirectory "C:\actions-runner" -Execute "PowerShell.exe" -Argument "-File C:\start-runner.ps1"
4968
$trigger = New-ScheduledTaskTrigger -AtStartup
5069
Register-ScheduledTask -TaskName "runnerinit" -Action $action -Trigger $trigger -User System -RunLevel Highest -Force
5170

52-
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule
71+
# TODO : The line below failed on my instances. I removed it, and everything works just fine. Something is funky here.
72+
# This doesn't work on Windows Server 2022. Only for Windows server 2019. Per this article https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2launch.html
73+
# C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule
74+
Write-Output "Finished running windows provisioner."

0 commit comments

Comments
 (0)