forked from fdcastel/Hyper-V-Automation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-UbuntuImage.ps1
More file actions
39 lines (29 loc) · 1.18 KB
/
Get-UbuntuImage.ps1
File metadata and controls
39 lines (29 loc) · 1.18 KB
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
[CmdletBinding()]
param(
[string]$OutputPath
)
$ErrorActionPreference = 'Stop'
# Note: Github removed TLS 1.0 support. Enables TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol-bor 'Tls12'
$urlRoot = 'https://cloud-images.ubuntu.com/releases/19.10/release'
$urlFile = 'ubuntu-19.10-server-cloudimg-amd64.img'
$url = "$urlRoot/$urlFile"
if (-not $OutputPath) {
$OutputPath = Get-Item '.\'
}
$imgFile = Join-Path $OutputPath $urlFile
if ([System.IO.File]::Exists($imgFile)) {
Write-Verbose "File '$imgFile' already exists. Nothing to do."
} else {
Write-Verbose "Downloading file '$imgFile'..."
$client = New-Object System.Net.WebClient
$client.DownloadFile($url, $imgFile)
Write-Verbose "Checking file integrity..."
$sha1Hash = Get-FileHash $imgFile -Algorithm SHA1
$allHashs = $client.DownloadString("$urlRoot/SHA1SUMS")
$m = [regex]::Matches($allHashs, "(?<Hash>\w{40})\s\*$urlFile")
if (-not $m[0]) { throw "Cannot get SHA1 hash for $urlFile." }
$expectedHash = $m[0].Groups['Hash'].Value
if ($sha1Hash.Hash -ne $expectedHash) { throw "Integrity check for '$imgFile' failed." }
}
$imgFile