-
-
Save developerprofiles/6c8f94a67e7eb657a52d4631ae67ceb9 to your computer and use it in GitHub Desktop.
Download and Install Terraform - Windows (PowerShell)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [CmdletBinding(DefaultParameterSetName='Version')] | |
| param | |
| ( | |
| [Parameter(ParameterSetName='Latest', Mandatory = $true)] | |
| [Parameter(ParameterSetName='Version', Mandatory = $true)] | |
| [String] | |
| $SaveToPath, | |
| [Parameter(ParameterSetName='Version', Mandatory = $true)] | |
| [String] | |
| $Version, | |
| [Parameter(ParameterSetName='Latest')] | |
| [Switch] | |
| $Latest | |
| ) | |
| try | |
| { | |
| if ($PSCmdlet.ParameterSetName -eq 'Version') | |
| { | |
| $downloadVersion = $Version | |
| } | |
| else | |
| { | |
| $releasesUrl = 'https://api.github.com/repos/hashicorp/terraform/releases' | |
| $releases = Invoke-RestMethod -Method Get -UseBasicParsing -Uri $releasesUrl | |
| $downloadVersion = $releases.Where({!$_.prerelease})[0].name.trim('v') | |
| } | |
| $terraformFile = "terraform_${downloadVersion}_windows_amd64.zip" | |
| $terraformURL = "https://releases.hashicorp.com/terraform/${downloadVersion}/${terraformFile}" | |
| $download = Invoke-WebRequest -UseBasicParsing -Uri $terraformURL -DisableKeepAlive -OutFile "${env:Temp}\${terraformFile}" -ErrorAction SilentlyContinue -PassThru | |
| if (($download.StatusCode -eq 200) -and (Test-Path "${env:Temp}\${terraformFile}")) | |
| { | |
| # If SaveToPath does not exist, create it | |
| if (-not (Test-Path -Path $SaveToPath)) | |
| { | |
| $null = New-Item -Path $SaveToPath -ItemType Directory -Force | |
| } | |
| # Unblock File | |
| Unblock-File "${env:Temp}\${terraformFile}" | |
| # Unpack archive | |
| Start-Sleep -Seconds 10 | |
| Expand-Archive -Path "${env:Temp}\${terraformFile}" -DestinationPath $SaveToPath -Force | |
| # Clean up temp folder | |
| Remove-Item -Path "${env:Temp}\${terraformFile}" -Force | |
| # Set up environment variable | |
| $path = [Environment]::GetEnvironmentVariable('Path','User') | |
| [Environment]::SetEnvironmentVariable('PATH', "${path};${SaveToPath}", 'User') | |
| } | |
| } | |
| catch | |
| { | |
| Write-Error $_ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment