Last active
June 19, 2020 23:27
-
-
Save motowilliams/830ecd8cd54924bd80b5b3e07bef67a5 to your computer and use it in GitHub Desktop.
This file contains 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()] | |
param ( | |
[string]$DirectoryPath, | |
[switch]$KeepDownload | |
) | |
process { | |
if ($DirectoryPath) { | |
Write-Verbose "User requested download location $DirectoryPath" | |
if (!(Test-Path -Path $DirectoryPath)) { | |
Write-Verbose "User requested download location $DirectoryPath does not exist" | |
New-Item -ItemType Directory -Path $DirectoryPath -Force | Out-Null | |
Write-Verbose "User requested download location $DirectoryPath created" | |
} | |
} | |
else { | |
Write-Verbose "Using script location, $PSScriptRoot, as download and install directory" | |
$DirectoryPath = $PSScriptRoot | |
} | |
$DirectoryPath = Resolve-Path -Path $DirectoryPath | |
$TerraformFullPath = Join-Path -Path $DirectoryPath -ChildPath "terraform.exe" | |
$downloadPageResponse = Invoke-WebRequest -UseBasicParsing -Uri "https://www.terraform.io/downloads.html" | |
[System.Uri]$downloadLink = $downloadPageResponse.Links | ` | |
Where-Object { $_.href -like "*windows*" -and $_.href -like "*64*" } | ` | |
Select-Object -ExpandProperty href | |
$fileName = $downloadLink.Segments | Select-Object -Last 1 | |
Write-Host "Downloading file $filename" | |
$fullArchivePath = (Join-Path -Path $DirectoryPath -ChildPath $fileName) | |
Invoke-WebRequest -UseBasicParsing -Uri $downloadLink -OutFile $fullArchivePath | |
if (Test-Path -Path $TerraformFullPath) { | |
Write-Host "Removing existing terraform.exe" | |
Remove-Item -Path $TerraformFullPath -ErrorAction Ignore | |
} | |
Write-Verbose "Unblocking file $fullArchivePath" | |
Unblock-File $fullArchivePath | |
Write-Host "Expanding archive $fullArchivePath to directory $DirectoryPath" | |
Expand-Archive -Path $fullArchivePath -DestinationPath $DirectoryPath | |
if (!$KeepDownload) { | |
Write-Verbose "Removing download archive $fullArchivePath" | |
Remove-Item -Force -Path $fullArchivePath | |
} | |
Write-Host "Testing Teraform (showing version information)" | |
& $TerraformFullPath --version | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment