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
|
# Terraform Installation Script for Windows
# Downloads and installs the latest Terraform from HashiCorp releases
if ($PSVersionTable.PSVersion.Major -lt 5 -or
($PSVersionTable.PSVersion.Major -eq 5 -and $PSVersionTable.PSVersion.Minor -lt 1)) {
Write-Host "This script requires PowerShell 5.1 or later" -ForegroundColor Red
Write-Host "Current version: $($PSVersionTable.PSVersion)" -ForegroundColor Red
exit 1
}
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$InstallDir = "$env:USERPROFILE\.local\bin"
$BinName = "terraform.exe"
function Write-Log {
param(
[ValidateSet("INF", "WRN", "ERR")]$Level = "INF",
[Parameter(Mandatory)][string]$Message
)
$colors = @{ INF = "Blue"; WRN = "Yellow"; ERR = "Red" }
Write-Host "$Level " -ForegroundColor $colors[$Level] -NoNewline
Write-Host $Message
}
function Get-Arch {
if (-not [Environment]::Is64BitOperatingSystem) {
Write-Log ERR "This script requires a 64-bit operating system"
exit 1
}
switch ($env:PROCESSOR_ARCHITECTURE) {
"AMD64" { return "amd64" }
"ARM64" { return "arm64" }
default {
Write-Log ERR "Unsupported architecture: $env:PROCESSOR_ARCHITECTURE"
exit 1
}
}
}
function Get-LatestVersion {
$Response = Invoke-RestMethod -Uri "https://checkpoint-api.hashicorp.com/v1/check/terraform"
return $Response.current_version
}
function Install-Terraform {
$Version = Get-LatestVersion
$Arch = Get-Arch
$ArchiveName = "terraform_${Version}_windows_${Arch}.zip"
$DownloadUrl = "https://releases.hashicorp.com/terraform/$Version/$ArchiveName"
$TempDir = Join-Path $env:TEMP "terraform-install"
$ArchivePath = Join-Path $TempDir $ArchiveName
Write-Log INF "Installing terraform $Version for $Arch"
Write-Log INF "Downloading from $DownloadUrl"
Write-Log INF "Installing to $InstallDir"
$ExistingBin = Join-Path $InstallDir $BinName
if (Test-Path $ExistingBin) {
$ExistingVersion = & $ExistingBin version -json 2>$null | ConvertFrom-Json | Select-Object -ExpandProperty terraform_version
Write-Log WRN "Found existing $ExistingVersion (will be upgraded to $Version)"
}
New-Item -ItemType Directory -Force -Path $TempDir | Out-Null
Write-Log INF "Downloading archive"
Invoke-WebRequest -Uri $DownloadUrl -OutFile $ArchivePath -UseBasicParsing
Expand-Archive -Path $ArchivePath -DestinationPath $TempDir -Force
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
$ExtractedBin = Join-Path $TempDir $BinName
if (-not (Test-Path $ExtractedBin)) {
Write-Log ERR "Extracted binary not found at expected location"
exit 1
}
Move-Item -Path $ExtractedBin -Destination (Join-Path $InstallDir $BinName) -Force
Write-Log INF "Installed terraform $Version to $InstallDir\$BinName"
Remove-Item -Path $TempDir -Recurse -Force
$CurrentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ([string]::IsNullOrEmpty($CurrentPath)) {
[Environment]::SetEnvironmentVariable("PATH", $InstallDir, "User")
Write-Log INF "Added $InstallDir to PATH (requires shell restart)"
}
else {
$PathEntries = $CurrentPath -split ';'
if ($PathEntries -notcontains $InstallDir) {
$NewPath = "$CurrentPath;$InstallDir"
[Environment]::SetEnvironmentVariable("PATH", $NewPath, "User")
Write-Log INF "Added $InstallDir to PATH (requires shell restart)"
}
}
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Green
Write-Host " 1. Restart your terminal"
Write-Host " 2. Run: 'terraform version' to verify the installation"
}
try {
Write-Host ""
Write-Host "Terraform Windows Installer" -ForegroundColor Green
Write-Host "Source: https://releases.hashicorp.com/terraform" -ForegroundColor Gray
Write-Host ""
Install-Terraform
}
catch {
Write-Host ""
Write-Log ERR "An error occurred when installing terraform"
Write-Host $_.Exception.Message -ForegroundColor Red
exit 1
}
|