Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
214 lines (190 loc) · 8.76 KB
/
Copy pathinstall.ps1
File metadata and controls
214 lines (190 loc) · 8.76 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# pswd-manager installer for Windows
# ----------------------------------
# Usage (PowerShell):
# irm https://raw.githubusercontent.com/Krish-Kamani/pswd-manager/main/install.ps1 | iex
#
# Always installs the LATEST GitHub release. No version is hardcoded.
# Prefers the .exe (NSIS) installer; falls back to the .msi (WiX) installer.
# Runs the downloaded installer silently and cleans up afterwards.
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
# PowerShell 5.1 defaults to TLS 1.0; GitHub requires TLS 1.2+.
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
}
catch { }
$script:Repo = 'Krish-Kamani/pswd-manager'
$script:ApiUrl = "https://api.github.com/repos/$($script:Repo)/releases/latest"
$script:ReleaseUrl = "https://github.com/$($script:Repo)/releases/latest"
$script:DownloadBase = "https://github.com/$($script:Repo)/releases/download"
function Write-Step {
param([string]$Message)
Write-Host "[pswd-manager] $Message"
}
function Get-PswdManagerRelease {
# Returns an object with Tag, Url, Name and (optional) Digest for the
# best Windows installer asset of the latest release.
$headers = @{ 'User-Agent' = 'pswd-manager-installer' }
$asset = $null
$tag = ''
try {
$release = Invoke-RestMethod -Uri $script:ApiUrl -Headers $headers
$tag = [string]$release.tag_name
$asset = $release.assets |
Where-Object { $_.name -like '*.exe' } |
Select-Object -First 1
if (-not $asset) {
$asset = $release.assets |
Where-Object { $_.name -like '*.msi' } |
Select-Object -First 1
}
if ($asset) {
return [pscustomobject]@{
Tag = $tag
Url = [string]$asset.browser_download_url
Name = [string]$asset.name
Digest = [string]$asset.digest
}
}
Write-Warning "Release $tag has no .exe or .msi asset; falling back to the release page."
}
catch {
Write-Warning "GitHub API request failed ($($_.Exception.Message)); falling back to the release page."
}
# Fallback: follow /releases/latest to discover the tag (no API rate limits),
# then read the asset list from the expanded_assets page.
$resp = $null
try {
$resp = Invoke-WebRequest -Uri $script:ReleaseUrl -MaximumRedirection 5 -UseBasicParsing
}
catch {
throw "Could not reach GitHub ($($script:ReleaseUrl)). Check your internet connection and try again."
}
$finalUri = $null
if ($resp.BaseResponse.ResponseUri) { $finalUri = $resp.BaseResponse.ResponseUri.AbsoluteUri }
elseif ($resp.BaseResponse.RequestMessage.RequestUri) { $finalUri = $resp.BaseResponse.RequestMessage.RequestUri.AbsoluteUri }
elseif ($resp.BaseResponse.RequestMessage.RequestUri.ToString()) { $finalUri = $resp.BaseResponse.RequestMessage.RequestUri.ToString() }
if ($finalUri -notmatch 'tag/(v?[^/]+)/?$') {
throw "Could not determine the latest release tag from $($script:ReleaseUrl)."
}
$tag = $Matches[1]
$page = Invoke-WebRequest -Uri "https://github.com/$($script:Repo)/releases/expanded_assets/$tag" -UseBasicParsing
$match = [regex]::Match($page.Content, 'href="([^"]+\.exe)"')
if (-not $match.Success) {
$match = [regex]::Match($page.Content, 'href="([^"]+\.msi)"')
}
if (-not $match.Success) {
throw "No .exe or .msi installer found in release $tag."
}
$url = $match.Groups[1].Value
if ($url -notlike 'https://github.com/*') { $url = "https://github.com$url" }
return [pscustomobject]@{
Tag = $tag
Url = $url
Name = [System.IO.Path]::GetFileName($url)
Digest = ''
}
}
function Test-PswdManagerInstalled {
# Returns $true once the application is detectable on disk or in the
# Windows uninstall registry. Polls because NSIS installers may relaunch
# themselves elevated (UAC), in which case Start-Process -Wait returns
# before the real installation has finished.
$candidates = @()
if ($env:LOCALAPPDATA) { $candidates += (Join-Path $env:LOCALAPPDATA 'pswd-manager') }
if ($env:ProgramFiles) { $candidates += (Join-Path $env:ProgramFiles 'pswd-manager') }
if (${env:ProgramFiles(x86)}) { $candidates += (Join-Path ${env:ProgramFiles(x86)} 'pswd-manager') }
for ($i = 0; $i -lt 60; $i++) {
foreach ($dir in $candidates) {
if (Test-Path $dir) {
if (Get-ChildItem -Path $dir -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.Name -in @('pswd-manager.exe', 'app.exe') }) {
return $true
}
}
}
$uninstall = Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
'HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' `
-ErrorAction SilentlyContinue |
Where-Object { $_.DisplayName -like 'pswd-manager*' }
if ($uninstall) { return $true }
Start-Sleep -Seconds 1
}
return $false
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
$tempDir = $null
try {
Write-Step 'Detecting operating system...'
if ($PSVersionTable.PSVersion.Major -ge 6) {
if ($IsLinux -or $IsMacOS) { throw 'Unsupported operating system. This installer supports Windows only.' }
}
Write-Step 'Detecting architecture...'
if (-not [Environment]::Is64BitOperatingSystem) {
throw "Unsupported architecture: 32-bit.
Currently pswd-manager provides x86_64 builds only."
}
Write-Step 'Detected x86_64.'
Write-Step 'Finding latest release...'
$release = Get-PswdManagerRelease
Write-Step "Latest version: $($release.Tag)"
# Security: only accept assets hosted by the official repository.
if ($release.Url -notlike "$($script:DownloadBase)/*") {
throw "Refusing to download from an unexpected source: $($release.Url)"
}
$tempDir = Join-Path $env:TEMP ("pswd-manager-" + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $tempDir | Out-Null
$installerPath = Join-Path $tempDir $release.Name
Write-Step "Downloading $($release.Name)..."
Invoke-WebRequest -Uri $release.Url -OutFile $installerPath -Headers @{ 'User-Agent' = 'pswd-manager-installer' } -UseBasicParsing
if (-not (Test-Path $installerPath) -or (Get-Item $installerPath).Length -lt 100KB) {
throw 'Download failed: the installer file is missing or too small.'
}
if ($release.Digest -match '^sha256:([0-9a-fA-F]{64})$') {
$expected = $Matches[1].ToLower()
$actual = (Get-FileHash -Algorithm SHA256 -Path $installerPath).Hash
if ($actual -ne $expected) {
throw "Checksum verification failed (expected sha256 $expected, got $actual). Aborting for safety."
}
Write-Step 'Download verified (sha256).'
}
Write-Step 'Installing...'
$process = $null
if ($installerPath -like '*.msi') {
# msiexec: /qn = quiet, /norestart = do not force a reboot.
$msiArgs = "/i `"$installerPath`" /qn /norestart"
$process = Start-Process -FilePath 'msiexec.exe' -ArgumentList $msiArgs -Wait -PassThru
}
else {
# NSIS: /S performs a silent install. The installer elevates itself if needed.
$process = Start-Process -FilePath $installerPath -ArgumentList '/S' -Wait -PassThru
}
# 0 = success, 3010 = success but a reboot is recommended.
$rebootRecommended = $false
if ($process.ExitCode -eq 3010) { $rebootRecommended = $true }
elseif ($process.ExitCode -ne 0) {
throw "Installer failed with exit code $($process.ExitCode). If you cancelled a UAC prompt, run the command again and accept it."
}
Write-Step 'Verifying installation...'
if (-not (Test-PswdManagerInstalled)) {
throw "The installer finished (exit code $($process.ExitCode)) but pswd-manager was not found on disk. The installation may have been cancelled or blocked."
}
if ($rebootRecommended) {
Write-Step 'Installation completed successfully. A reboot is recommended before first use.'
}
else {
Write-Step 'Installation completed successfully.'
}
}
catch {
Write-Host -ForegroundColor Red "[pswd-manager] ERROR: $($_.Exception.Message)"
}
finally {
if ($tempDir -and (Test-Path $tempDir)) {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Step 'Temporary files removed.'
}
}