-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
86 lines (71 loc) · 2.32 KB
/
Copy pathinstall.ps1
File metadata and controls
86 lines (71 loc) · 2.32 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
# GSwitch Windows installer helper.
# This script downloads the NSIS installer from the latest public GitHub Release.
[CmdletBinding()]
param()
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$Repository = 'ginbing/GSwitch'
$ReleasesPage = "https://github.com/$Repository/releases"
$ReleaseApi = "https://api.github.com/repos/$Repository/releases/latest"
$GitHubHeaders = @{
Accept = 'application/vnd.github+json'
'User-Agent' = 'GSwitch-installer'
'X-GitHub-Api-Version' = '2022-11-28'
}
function Select-GSwitchInstallerAsset {
[CmdletBinding()]
param(
[object[]]$Assets
)
$matches = @(
$Assets | Where-Object {
$_.name -is [string] -and $_.name -match '(?i)-setup\.exe$'
}
)
if ($matches.Count -ne 1) {
$available = @($Assets | ForEach-Object { [string]$_.name }) -join ', '
if ([string]::IsNullOrWhiteSpace($available)) {
$available = 'none'
}
throw "Expected exactly one Windows NSIS setup.exe asset; found $($matches.Count). Available assets: $available"
}
return $matches[0]
}
function Install-GSwitch {
[CmdletBinding()]
param()
try {
$release = Invoke-RestMethod -Uri $ReleaseApi -Headers $GitHubHeaders
}
catch {
throw "Could not find the latest public GSwitch release. Try again later or download the Windows installer from $ReleasesPage."
}
$asset = Select-GSwitchInstallerAsset -Assets @($release.assets)
$downloadUrl = [string]$asset.browser_download_url
if ([string]::IsNullOrWhiteSpace($downloadUrl)) {
throw 'The selected GSwitch installer does not have a download URL.'
}
$installerPath = Join-Path ([System.IO.Path]::GetTempPath()) (
"GSwitch-$([System.Guid]::NewGuid().ToString('N'))-setup.exe"
)
try {
try {
Invoke-WebRequest -Uri $downloadUrl -Headers $GitHubHeaders -OutFile $installerPath
}
catch {
throw "Could not download the GSwitch installer. Try again or download it from $ReleasesPage."
}
$installer = Start-Process -FilePath $installerPath -Wait -PassThru
if ($installer.ExitCode -ne 0) {
throw "The GSwitch installer failed with exit code $($installer.ExitCode)."
}
}
finally {
if (Test-Path -LiteralPath $installerPath) {
Remove-Item -LiteralPath $installerPath -Force
}
}
}
if ($MyInvocation.InvocationName -ne '.') {
Install-GSwitch
}