-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstall-Latest-PowerShell7-Win64.ps1
More file actions
130 lines (101 loc) · 3.34 KB
/
Install-Latest-PowerShell7-Win64.ps1
File metadata and controls
130 lines (101 loc) · 3.34 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
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
<#
.SYNOPSIS
Downloads and install the latest PowerShell 7 win-x64 release from GitHub.
.DESCRIPTION
Downloads the latest PowerShell 7 win-x64 release from GitHub
into the user's Downloads directory and executes the installer.
.NOTES
Author: Jonathan Panning <jpann [at] impostr-labs.com>
Filename: Install-Latest-PowerShell7-Win64.ps1
Created on: 01-16-2024
Last updated: 01-16-2024
.PARAMETER DownloadOnly
Boolean. Only download the installer.
.INPUTS
None.
.OUTPUTS
None. Install-Latest-PowerShell7-Win64.ps1 does not generate any output.
.EXAMPLE
PS> .\Install-Latest-PowerShell7-Win64.ps1
.LINK
https://github.com/jpann
#>
#requires -RunAsAdministrator
#requires -version 4
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[switch]$DownloadOnly
)
BEGIN {
function Get-Latest-Version {
Param(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$Url
)
Write-Verbose "URL: $Url"
$tag = (Invoke-WebRequest -UseBasicParsing $Url| ConvertFrom-Json)[0].tag_name
$tagUrl = (Invoke-WebRequest -UseBasicParsing $Url| ConvertFrom-Json)[0].html_url
Write-Verbose "Tag URL: $tagUrl"
$tag
}
function Get-Latest-Assets {
Param(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$Url
)
$assetsObject = (Invoke-WebRequest -UseBasicParsing $Url| ConvertFrom-Json)[0].assets
$assets = @( )
foreach ($asset in $assetsObject) {
$assetName = $asset.name
$assetUrl = $asset.browser_download_url
$assets += @{
Name = $assetName;
Url = $assetUrl
}
}
$assets
}
function Download {
Param(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$Url,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$File
)
Write-Verbose "File: $File"
Write-Verbose "URL: $Url"
Invoke-WebRequest -Method Get -Uri:$Url -OutFile $File
}
$Script:REPO = "PowerShell/PowerShell"
$Script:RELEASES = "https://api.github.com/repos/$Script:REPO/releases/latest"
}
PROCESS {
Write-Host "Determining latest release ..."
$tag = Get-Latest-Version -Url $Script:RELEASES
Write-Host "Latest release is " -NoNewLine
Write-Host "$tag" -ForegroundColor Green
$assets = Get-Latest-Assets -Url $Script:RELEASES
Write-Host "This release has " -NoNewLine
Write-Host "$($assets.Count)" -ForegroundColor Green -NoNewLine
Write-Host " files"
$fileName = "PowerShell-{0}-win-x64.msi" -f $tag.Replace("-Beta", "").Replace("p1", "").Replace("v", "")
$filePath = Join-Path (Join-Path $HOME "Downloads") $fileName
$url = "https://github.com/$($Script:REPO)/releases/download/$tag/$fileName"
Write-Host ">> Downloading $fileName to $($filePath) ..."
Download -File $filePath -Url $url
if ($DownloadOnly) {
# Launch installer
if (Test-Path -Path "$filePath" -PathType Leaf) {
Write-Host "Launching installer $filePath ..."
# Install
msiexec /i $filePath
} else {
Write-Error "Installer $filePath does not exist!"
}
}
}