Download-MicrosoftEdge.ps1

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
123
124
125
126
127
128
129
130

<#PSScriptInfo
 
.VERSION 1.0.1
 
.GUID 86f1ab54-a34b-42e9-9a67-09ff4992e897
 
.AUTHOR Andre Hohenstein Contact: a.hohenstein@outlook.com / https://twitter.com/AndreHohenstein
 
.COMPANYNAME Andre Hohenstein IT-Consulting & Training
 
.COPYRIGHT � 2020 by Andr� Hohenstein - Alle Rechte vorbehalten
 
.TAGS Edge Script PowerShell Browser Download Microsoft Stable Dev Beta Policy Automation
 
.LICENSEURI
 
.PROJECTURI https://github.com/AndreHohenstein/Edge
 
.ICONURI https://raw.githubusercontent.com/AndreHohenstein/Edge/main/media/powershell.png
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
1.0.0 Initial .ps1 script version of Download-MicrosoftEdge
1.0.1 Change in Invoke-WebRequest UseBasicParsing added and new Project-URL
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Download Microsoft Edge with PowerShell 5.1 or PowerShellCore 7.03
 
#>
 
[CmdletBinding()]
param(
  [Parameter(Mandatory = $True, HelpMessage = 'Channel to download, Valid Options are: Dev, Beta, Stable, EdgeUpdate, Policy')]
  [ValidateSet('Dev', 'Beta', 'Stable', 'EdgeUpdate', 'Policy')]
  [string]$Channel,    
  [Parameter(Mandatory = $True, HelpMessage = 'Folder where the file will be downloaded')]
  [ValidateNotNullOrEmpty()]
  [string]$Folder,
  [Parameter(Mandatory = $false, HelpMessage = 'Platform to download, Valid Options are: Windows or MacOS')]
  [ValidateSet('Windows', 'MacOS', 'any')]
  [string]$Platform = "Windows",
  [Parameter(Mandatory = $false, HelpMessage = "Architecture to download, Valid Options are: x86, x64, arm64, any")]
  [ValidateSet('x86', 'x64', 'arm64', 'any')]
  [string]$Architecture = "x64",
  [parameter(Mandatory = $false, HelpMessage = "Specifies which version to download")]
  [ValidateNotNullOrEmpty()]
  [string]$ProductVersion,
  [parameter(Mandatory = $false, HelpMessage = "Overwrites the file without asking")]
  [Switch]$Force
)

Write-Host "Getting available files from https://edgeupdates.microsoft.com/api/products?view=enterprise" -ForegroundColor Green
$response = Invoke-WebRequest -Uri "https://edgeupdates.microsoft.com/api/products?view=enterprise" -Method Get -ContentType "application/json" -ErrorAction Stop -UseBasicParsing
$jsonObj = ConvertFrom-Json $([String]::new($response.Content))
Write-Host "Succefully retrived data" -ForegroundColor Green

$SelectedIndex = [array]::indexof($jsonObj.Product, "$Channel")

if ([string]::IsNullOrEmpty($ProductVersion)) {
  Write-host "No version specified, getting the latest for $Channel" -ForegroundColor Green
  $SelectedVersion = (([Version[]](($jsonObj[$SelectedIndex].Releases | Where-Object { $_.Architecture -eq $Architecture -and $_.Platform -eq $Platform }).ProductVersion) | Sort-Object -Descending)[0]).ToString(4)
  Write-Host "Latest Version for Chanel $Channel is $SelectedVersion" -ForegroundColor Green
  $SelectedObject = $jsonObj[$SelectedIndex].Releases | Where-Object { $_.Architecture -eq $Architecture -and $_.Platform -eq $Platform -and $_.ProductVersion -eq $SelectedVersion }
}
else {
  Write-Host "Matching $ProductVersion on Channel $Channel" -ForegroundColor Green
  $SelectedObject = ($jsonObj[$SelectedIndex].Releases | Where-Object { $_.Architecture -eq $Architecture -and $_.Platform -eq $Platform -and $_.ProductVersion -eq $ProductVersion })
  $SelectedObject
  If ($null -eq $SelectedObject) {
    Write-Host "No version matching $ProductVersion found using Channel $channel and Arch $Architecture!" -ForegroundColor Red
    break
  }
  else {
    Write-Host "Found matchings version" -ForegroundColor Green
    $SelectedObject
  }
}

$FileName = ($SelectedObject.Artifacts.Location -split "/")[-1]
Write-Host "File to be downloaded $FileName" -ForegroundColor Green
Write-host "Starting download of $($SelectedObject.Artifacts.Location)" -ForegroundColor Green
if (Test-Path $Folder) {
  if (Test-Path "$Folder\$FileName" -ErrorAction SilentlyContinue) {
    If ($Force) {
      Write-Host "Using Force and to Download and overwrite existing file." -ForegroundColor Green
      Invoke-WebRequest -Uri $SelectedObject.Artifacts.Location -OutFile "$Folder\$FileName" -ErrorAction Stop  
    }
    else {
      Write-Host "$Folder\$FileName already exists!" -ForegroundColor Yellow
      $OverWrite = Read-Host -Prompt "Press Y to overwrite or N to quit." 
      if ($OverWrite -eq "Y") {
        Write-Host "Starting Download" -ForegroundColor Green
        Invoke-WebRequest -Uri $SelectedObject.Artifacts.Location -OutFile "$Folder\$FileName" -ErrorAction Stop
      }
      else {
        Write-Host "User aborted, will not overwrite file." -ForegroundColor Red  
        break
      }
    }
  }
  else {
    Invoke-WebRequest -Uri $SelectedObject.Artifacts.Location -OutFile "$Folder\$FileName" -ErrorAction Stop
  }
  
}
else {
  Write-Host "Folder $Folder does not exist" -ForegroundColor Red
  break
}

if (((Get-FileHash -Algorithm $SelectedObject.Artifacts.HashAlgorithm -Path "$Folder\$FileName").Hash) -eq $SelectedObject.Artifacts.Hash) {
  Write-Host "CheckSum OK" -ForegroundColor Green
}
else {
  Write-host "Checksum mismatch!" -ForegroundColor Red
  Write-Host "Expected Hash : $($SelectedObject.Artifacts.Hash)" -ForegroundColor Yellow
  Write-Host "Downloaded file Hash : $((Get-FileHash -Algorithm $SelectedObject.Artifacts.HashAlgorithm -Path "$Folder\$FileName").Hash)" -ForegroundColor Yellow
}
Write-Host " -- Completed --" -ForegroundColor Green