From b8aeb402c41896d832776531c9ac96bebb521b73 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 01:41:55 +0000 Subject: [PATCH] shortcut: retry and fall back when the download fails, keep the old copy Turning the shortcut on now downloads to a temp file with retries and a raw.githubusercontent.com fallback, and restores the previous copy if every attempt fails. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01DU3cH2Fs1FdP46uJBSom5X --- AGENTS.md | 2 +- apps/appupdate.ps1 | 37 +++++++++++++++++++++++++++++++------ apps/filemap.ps1 | 37 +++++++++++++++++++++++++++++++------ apps/filesync.ps1 | 37 +++++++++++++++++++++++++++++++------ apps/systeminfo.ps1 | 37 +++++++++++++++++++++++++++++++------ apps/zipper.ps1 | 37 +++++++++++++++++++++++++++++++------ scripts/sync-blocks.sh | 37 +++++++++++++++++++++++++++++++------ 7 files changed, 187 insertions(+), 37 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 80769ff..a9752bb 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -35,7 +35,7 @@ always work. Proxmox tools: `therepos/proxmox`; MCP servers: `therepos/mcp`; Cla hidden or scheduled run) puts the window on the right half of its screen. - `shortcut`: every script with a menu offers Settings > `Shortcut '' [on/off]`. It saves a local copy in `%LOCALAPPDATA%\` and adds `function { & "$env:LOCALAPPDATA\\.ps1" }` - to `$PROFILE`; nothing is fetched when it runs, and off/on refreshes the copy. Use `Show-ShortcutSettings` + to `$PROFILE`; nothing is fetched when it runs, and off/on downloads the latest copy (keeping the old one if the download fails). Use `Show-ShortcutSettings` when the shortcut is the only setting, else `Test-Shortcut` / `Switch-Shortcut` in the script's Settings. - Per-tool files (tools, settings, logs, local copy) go in `%LOCALAPPDATA%\`, fetched without admin. - Secrets: never hard-coded; ask once, save DPAPI-encrypted (`ConvertFrom-SecureString`) in `settings.json`. diff --git a/apps/appupdate.ps1 b/apps/appupdate.ps1 index 1c15bee..97a7d4b 100644 --- a/apps/appupdate.ps1 +++ b/apps/appupdate.ps1 @@ -630,6 +630,24 @@ function Test-Shortcut { (Test-Path -LiteralPath $PROFILE) -and (Test-Path -LiteralPath (Get-ShortcutCopy)) -and (@(Get-Content -LiteralPath $PROFILE -ErrorAction SilentlyContinue) -contains (Get-ShortcutLine)) } +# Downloads the latest script over the local copy; the copy is only replaced by a complete download +function Save-ShortcutCopy($copy) { + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + $tmp = "$copy.new" + $raw = $ScriptUrl -replace '^https://github\.com/([^/]+/[^/]+)/raw/', 'https://raw.githubusercontent.com/$1/' + foreach ($url in $ScriptUrl, $raw, $ScriptUrl) { + try { + Invoke-WebRequest -Uri "${url}?$(Get-Random)" -OutFile $tmp -UseBasicParsing -ErrorAction Stop + if (Select-String -LiteralPath $tmp -SimpleMatch '$ScriptUrl' -Quiet) { + Move-Item -LiteralPath $tmp -Destination $copy -Force + return $true + } + } catch {} + Start-Sleep -Seconds 2 + } + Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue + return $false +} function Switch-Shortcut([switch]$KeepCopy) { $name = Get-ShortcutName $copy = Get-ShortcutCopy @@ -640,16 +658,22 @@ function Switch-Shortcut([switch]$KeepCopy) { @(Get-Content -LiteralPath $PROFILE) | Where-Object { $_ -notmatch "^function $name " } | Set-Content -LiteralPath $PROFILE } if ($on) { - if (-not $KeepCopy) { Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue } + if (-not $KeepCopy) { + # Kept in memory so a failed download on the next "on" can restore it + if (Test-Path -LiteralPath $copy) { $script:ShortcutKept = [IO.File]::ReadAllBytes($copy) } + Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue + } Say " Shortcut removed." 'Green' return } New-Item -ItemType Directory -Path (Split-Path -Path $copy -Parent) -Force | Out-Null - if ($PSCommandPath -and (Test-Path -LiteralPath $PSCommandPath)) { - if ($PSCommandPath -ne $copy) { Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force } - } else { - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - Invoke-WebRequest -Uri "${ScriptUrl}?$(Get-Random)" -OutFile $copy -UseBasicParsing + $stale = $false + if ($PSCommandPath -and ($PSCommandPath -ne $copy) -and (Test-Path -LiteralPath $PSCommandPath)) { + Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force + } elseif (-not (Save-ShortcutCopy $copy)) { + if ($script:ShortcutKept -and -not (Test-Path -LiteralPath $copy)) { [IO.File]::WriteAllBytes($copy, $script:ShortcutKept) } + if (-not (Test-Path -LiteralPath $copy)) { throw 'the download failed. Check the connection and try again.' } + $stale = $true } Unblock-File -LiteralPath $copy -ErrorAction SilentlyContinue if ((Get-ExecutionPolicy -Scope CurrentUser) -in 'Undefined', 'Restricted', 'AllSigned') { @@ -658,6 +682,7 @@ function Switch-Shortcut([switch]$KeepCopy) { if (-not (Test-Path -LiteralPath $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force | Out-Null } Add-Content -LiteralPath $PROFILE "`n$(Get-ShortcutLine)" Say " Shortcut added. In a new PowerShell window, just type: $name" 'Green' + if ($stale) { Say " Could not download the latest version; kept the previous copy." 'Yellow' } } catch { Say " Could not change the shortcut: $($_.Exception.Message)" 'Red' } diff --git a/apps/filemap.ps1 b/apps/filemap.ps1 index 07b4250..a0b5805 100644 --- a/apps/filemap.ps1 +++ b/apps/filemap.ps1 @@ -77,6 +77,24 @@ function Test-Shortcut { (Test-Path -LiteralPath $PROFILE) -and (Test-Path -LiteralPath (Get-ShortcutCopy)) -and (@(Get-Content -LiteralPath $PROFILE -ErrorAction SilentlyContinue) -contains (Get-ShortcutLine)) } +# Downloads the latest script over the local copy; the copy is only replaced by a complete download +function Save-ShortcutCopy($copy) { + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + $tmp = "$copy.new" + $raw = $ScriptUrl -replace '^https://github\.com/([^/]+/[^/]+)/raw/', 'https://raw.githubusercontent.com/$1/' + foreach ($url in $ScriptUrl, $raw, $ScriptUrl) { + try { + Invoke-WebRequest -Uri "${url}?$(Get-Random)" -OutFile $tmp -UseBasicParsing -ErrorAction Stop + if (Select-String -LiteralPath $tmp -SimpleMatch '$ScriptUrl' -Quiet) { + Move-Item -LiteralPath $tmp -Destination $copy -Force + return $true + } + } catch {} + Start-Sleep -Seconds 2 + } + Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue + return $false +} function Switch-Shortcut([switch]$KeepCopy) { $name = Get-ShortcutName $copy = Get-ShortcutCopy @@ -87,16 +105,22 @@ function Switch-Shortcut([switch]$KeepCopy) { @(Get-Content -LiteralPath $PROFILE) | Where-Object { $_ -notmatch "^function $name " } | Set-Content -LiteralPath $PROFILE } if ($on) { - if (-not $KeepCopy) { Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue } + if (-not $KeepCopy) { + # Kept in memory so a failed download on the next "on" can restore it + if (Test-Path -LiteralPath $copy) { $script:ShortcutKept = [IO.File]::ReadAllBytes($copy) } + Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue + } Say " Shortcut removed." 'Green' return } New-Item -ItemType Directory -Path (Split-Path -Path $copy -Parent) -Force | Out-Null - if ($PSCommandPath -and (Test-Path -LiteralPath $PSCommandPath)) { - if ($PSCommandPath -ne $copy) { Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force } - } else { - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - Invoke-WebRequest -Uri "${ScriptUrl}?$(Get-Random)" -OutFile $copy -UseBasicParsing + $stale = $false + if ($PSCommandPath -and ($PSCommandPath -ne $copy) -and (Test-Path -LiteralPath $PSCommandPath)) { + Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force + } elseif (-not (Save-ShortcutCopy $copy)) { + if ($script:ShortcutKept -and -not (Test-Path -LiteralPath $copy)) { [IO.File]::WriteAllBytes($copy, $script:ShortcutKept) } + if (-not (Test-Path -LiteralPath $copy)) { throw 'the download failed. Check the connection and try again.' } + $stale = $true } Unblock-File -LiteralPath $copy -ErrorAction SilentlyContinue if ((Get-ExecutionPolicy -Scope CurrentUser) -in 'Undefined', 'Restricted', 'AllSigned') { @@ -105,6 +129,7 @@ function Switch-Shortcut([switch]$KeepCopy) { if (-not (Test-Path -LiteralPath $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force | Out-Null } Add-Content -LiteralPath $PROFILE "`n$(Get-ShortcutLine)" Say " Shortcut added. In a new PowerShell window, just type: $name" 'Green' + if ($stale) { Say " Could not download the latest version; kept the previous copy." 'Yellow' } } catch { Say " Could not change the shortcut: $($_.Exception.Message)" 'Red' } diff --git a/apps/filesync.ps1 b/apps/filesync.ps1 index 458ec3a..f79493e 100644 --- a/apps/filesync.ps1 +++ b/apps/filesync.ps1 @@ -155,6 +155,24 @@ function Test-Shortcut { (Test-Path -LiteralPath $PROFILE) -and (Test-Path -LiteralPath (Get-ShortcutCopy)) -and (@(Get-Content -LiteralPath $PROFILE -ErrorAction SilentlyContinue) -contains (Get-ShortcutLine)) } +# Downloads the latest script over the local copy; the copy is only replaced by a complete download +function Save-ShortcutCopy($copy) { + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + $tmp = "$copy.new" + $raw = $ScriptUrl -replace '^https://github\.com/([^/]+/[^/]+)/raw/', 'https://raw.githubusercontent.com/$1/' + foreach ($url in $ScriptUrl, $raw, $ScriptUrl) { + try { + Invoke-WebRequest -Uri "${url}?$(Get-Random)" -OutFile $tmp -UseBasicParsing -ErrorAction Stop + if (Select-String -LiteralPath $tmp -SimpleMatch '$ScriptUrl' -Quiet) { + Move-Item -LiteralPath $tmp -Destination $copy -Force + return $true + } + } catch {} + Start-Sleep -Seconds 2 + } + Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue + return $false +} function Switch-Shortcut([switch]$KeepCopy) { $name = Get-ShortcutName $copy = Get-ShortcutCopy @@ -165,16 +183,22 @@ function Switch-Shortcut([switch]$KeepCopy) { @(Get-Content -LiteralPath $PROFILE) | Where-Object { $_ -notmatch "^function $name " } | Set-Content -LiteralPath $PROFILE } if ($on) { - if (-not $KeepCopy) { Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue } + if (-not $KeepCopy) { + # Kept in memory so a failed download on the next "on" can restore it + if (Test-Path -LiteralPath $copy) { $script:ShortcutKept = [IO.File]::ReadAllBytes($copy) } + Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue + } Say " Shortcut removed." 'Green' return } New-Item -ItemType Directory -Path (Split-Path -Path $copy -Parent) -Force | Out-Null - if ($PSCommandPath -and (Test-Path -LiteralPath $PSCommandPath)) { - if ($PSCommandPath -ne $copy) { Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force } - } else { - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - Invoke-WebRequest -Uri "${ScriptUrl}?$(Get-Random)" -OutFile $copy -UseBasicParsing + $stale = $false + if ($PSCommandPath -and ($PSCommandPath -ne $copy) -and (Test-Path -LiteralPath $PSCommandPath)) { + Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force + } elseif (-not (Save-ShortcutCopy $copy)) { + if ($script:ShortcutKept -and -not (Test-Path -LiteralPath $copy)) { [IO.File]::WriteAllBytes($copy, $script:ShortcutKept) } + if (-not (Test-Path -LiteralPath $copy)) { throw 'the download failed. Check the connection and try again.' } + $stale = $true } Unblock-File -LiteralPath $copy -ErrorAction SilentlyContinue if ((Get-ExecutionPolicy -Scope CurrentUser) -in 'Undefined', 'Restricted', 'AllSigned') { @@ -183,6 +207,7 @@ function Switch-Shortcut([switch]$KeepCopy) { if (-not (Test-Path -LiteralPath $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force | Out-Null } Add-Content -LiteralPath $PROFILE "`n$(Get-ShortcutLine)" Say " Shortcut added. In a new PowerShell window, just type: $name" 'Green' + if ($stale) { Say " Could not download the latest version; kept the previous copy." 'Yellow' } } catch { Say " Could not change the shortcut: $($_.Exception.Message)" 'Red' } diff --git a/apps/systeminfo.ps1 b/apps/systeminfo.ps1 index 99bf38d..b96ff9d 100644 --- a/apps/systeminfo.ps1 +++ b/apps/systeminfo.ps1 @@ -1647,6 +1647,24 @@ function Test-Shortcut { (Test-Path -LiteralPath $PROFILE) -and (Test-Path -LiteralPath (Get-ShortcutCopy)) -and (@(Get-Content -LiteralPath $PROFILE -ErrorAction SilentlyContinue) -contains (Get-ShortcutLine)) } +# Downloads the latest script over the local copy; the copy is only replaced by a complete download +function Save-ShortcutCopy($copy) { + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + $tmp = "$copy.new" + $raw = $ScriptUrl -replace '^https://github\.com/([^/]+/[^/]+)/raw/', 'https://raw.githubusercontent.com/$1/' + foreach ($url in $ScriptUrl, $raw, $ScriptUrl) { + try { + Invoke-WebRequest -Uri "${url}?$(Get-Random)" -OutFile $tmp -UseBasicParsing -ErrorAction Stop + if (Select-String -LiteralPath $tmp -SimpleMatch '$ScriptUrl' -Quiet) { + Move-Item -LiteralPath $tmp -Destination $copy -Force + return $true + } + } catch {} + Start-Sleep -Seconds 2 + } + Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue + return $false +} function Switch-Shortcut([switch]$KeepCopy) { $name = Get-ShortcutName $copy = Get-ShortcutCopy @@ -1657,16 +1675,22 @@ function Switch-Shortcut([switch]$KeepCopy) { @(Get-Content -LiteralPath $PROFILE) | Where-Object { $_ -notmatch "^function $name " } | Set-Content -LiteralPath $PROFILE } if ($on) { - if (-not $KeepCopy) { Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue } + if (-not $KeepCopy) { + # Kept in memory so a failed download on the next "on" can restore it + if (Test-Path -LiteralPath $copy) { $script:ShortcutKept = [IO.File]::ReadAllBytes($copy) } + Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue + } Say " Shortcut removed." 'Green' return } New-Item -ItemType Directory -Path (Split-Path -Path $copy -Parent) -Force | Out-Null - if ($PSCommandPath -and (Test-Path -LiteralPath $PSCommandPath)) { - if ($PSCommandPath -ne $copy) { Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force } - } else { - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - Invoke-WebRequest -Uri "${ScriptUrl}?$(Get-Random)" -OutFile $copy -UseBasicParsing + $stale = $false + if ($PSCommandPath -and ($PSCommandPath -ne $copy) -and (Test-Path -LiteralPath $PSCommandPath)) { + Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force + } elseif (-not (Save-ShortcutCopy $copy)) { + if ($script:ShortcutKept -and -not (Test-Path -LiteralPath $copy)) { [IO.File]::WriteAllBytes($copy, $script:ShortcutKept) } + if (-not (Test-Path -LiteralPath $copy)) { throw 'the download failed. Check the connection and try again.' } + $stale = $true } Unblock-File -LiteralPath $copy -ErrorAction SilentlyContinue if ((Get-ExecutionPolicy -Scope CurrentUser) -in 'Undefined', 'Restricted', 'AllSigned') { @@ -1675,6 +1699,7 @@ function Switch-Shortcut([switch]$KeepCopy) { if (-not (Test-Path -LiteralPath $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force | Out-Null } Add-Content -LiteralPath $PROFILE "`n$(Get-ShortcutLine)" Say " Shortcut added. In a new PowerShell window, just type: $name" 'Green' + if ($stale) { Say " Could not download the latest version; kept the previous copy." 'Yellow' } } catch { Say " Could not change the shortcut: $($_.Exception.Message)" 'Red' } diff --git a/apps/zipper.ps1 b/apps/zipper.ps1 index 826b96e..d937747 100644 --- a/apps/zipper.ps1 +++ b/apps/zipper.ps1 @@ -254,6 +254,24 @@ function Test-Shortcut { (Test-Path -LiteralPath $PROFILE) -and (Test-Path -LiteralPath (Get-ShortcutCopy)) -and (@(Get-Content -LiteralPath $PROFILE -ErrorAction SilentlyContinue) -contains (Get-ShortcutLine)) } +# Downloads the latest script over the local copy; the copy is only replaced by a complete download +function Save-ShortcutCopy($copy) { + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + $tmp = "$copy.new" + $raw = $ScriptUrl -replace '^https://github\.com/([^/]+/[^/]+)/raw/', 'https://raw.githubusercontent.com/$1/' + foreach ($url in $ScriptUrl, $raw, $ScriptUrl) { + try { + Invoke-WebRequest -Uri "${url}?$(Get-Random)" -OutFile $tmp -UseBasicParsing -ErrorAction Stop + if (Select-String -LiteralPath $tmp -SimpleMatch '$ScriptUrl' -Quiet) { + Move-Item -LiteralPath $tmp -Destination $copy -Force + return $true + } + } catch {} + Start-Sleep -Seconds 2 + } + Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue + return $false +} function Switch-Shortcut([switch]$KeepCopy) { $name = Get-ShortcutName $copy = Get-ShortcutCopy @@ -264,16 +282,22 @@ function Switch-Shortcut([switch]$KeepCopy) { @(Get-Content -LiteralPath $PROFILE) | Where-Object { $_ -notmatch "^function $name " } | Set-Content -LiteralPath $PROFILE } if ($on) { - if (-not $KeepCopy) { Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue } + if (-not $KeepCopy) { + # Kept in memory so a failed download on the next "on" can restore it + if (Test-Path -LiteralPath $copy) { $script:ShortcutKept = [IO.File]::ReadAllBytes($copy) } + Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue + } Say " Shortcut removed." 'Green' return } New-Item -ItemType Directory -Path (Split-Path -Path $copy -Parent) -Force | Out-Null - if ($PSCommandPath -and (Test-Path -LiteralPath $PSCommandPath)) { - if ($PSCommandPath -ne $copy) { Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force } - } else { - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - Invoke-WebRequest -Uri "${ScriptUrl}?$(Get-Random)" -OutFile $copy -UseBasicParsing + $stale = $false + if ($PSCommandPath -and ($PSCommandPath -ne $copy) -and (Test-Path -LiteralPath $PSCommandPath)) { + Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force + } elseif (-not (Save-ShortcutCopy $copy)) { + if ($script:ShortcutKept -and -not (Test-Path -LiteralPath $copy)) { [IO.File]::WriteAllBytes($copy, $script:ShortcutKept) } + if (-not (Test-Path -LiteralPath $copy)) { throw 'the download failed. Check the connection and try again.' } + $stale = $true } Unblock-File -LiteralPath $copy -ErrorAction SilentlyContinue if ((Get-ExecutionPolicy -Scope CurrentUser) -in 'Undefined', 'Restricted', 'AllSigned') { @@ -282,6 +306,7 @@ function Switch-Shortcut([switch]$KeepCopy) { if (-not (Test-Path -LiteralPath $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force | Out-Null } Add-Content -LiteralPath $PROFILE "`n$(Get-ShortcutLine)" Say " Shortcut added. In a new PowerShell window, just type: $name" 'Green' + if ($stale) { Say " Could not download the latest version; kept the previous copy." 'Yellow' } } catch { Say " Could not change the shortcut: $($_.Exception.Message)" 'Red' } diff --git a/scripts/sync-blocks.sh b/scripts/sync-blocks.sh index c44edef..258d798 100755 --- a/scripts/sync-blocks.sh +++ b/scripts/sync-blocks.sh @@ -188,6 +188,24 @@ function Test-Shortcut { (Test-Path -LiteralPath $PROFILE) -and (Test-Path -LiteralPath (Get-ShortcutCopy)) -and (@(Get-Content -LiteralPath $PROFILE -ErrorAction SilentlyContinue) -contains (Get-ShortcutLine)) } +# Downloads the latest script over the local copy; the copy is only replaced by a complete download +function Save-ShortcutCopy($copy) { + [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 + $tmp = "$copy.new" + $raw = $ScriptUrl -replace '^https://github\.com/([^/]+/[^/]+)/raw/', 'https://raw.githubusercontent.com/$1/' + foreach ($url in $ScriptUrl, $raw, $ScriptUrl) { + try { + Invoke-WebRequest -Uri "${url}?$(Get-Random)" -OutFile $tmp -UseBasicParsing -ErrorAction Stop + if (Select-String -LiteralPath $tmp -SimpleMatch '$ScriptUrl' -Quiet) { + Move-Item -LiteralPath $tmp -Destination $copy -Force + return $true + } + } catch {} + Start-Sleep -Seconds 2 + } + Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue + return $false +} function Switch-Shortcut([switch]$KeepCopy) { $name = Get-ShortcutName $copy = Get-ShortcutCopy @@ -198,16 +216,22 @@ function Switch-Shortcut([switch]$KeepCopy) { @(Get-Content -LiteralPath $PROFILE) | Where-Object { $_ -notmatch "^function $name " } | Set-Content -LiteralPath $PROFILE } if ($on) { - if (-not $KeepCopy) { Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue } + if (-not $KeepCopy) { + # Kept in memory so a failed download on the next "on" can restore it + if (Test-Path -LiteralPath $copy) { $script:ShortcutKept = [IO.File]::ReadAllBytes($copy) } + Remove-Item -LiteralPath $copy -Force -ErrorAction SilentlyContinue + } Say " Shortcut removed." 'Green' return } New-Item -ItemType Directory -Path (Split-Path -Path $copy -Parent) -Force | Out-Null - if ($PSCommandPath -and (Test-Path -LiteralPath $PSCommandPath)) { - if ($PSCommandPath -ne $copy) { Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force } - } else { - [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 - Invoke-WebRequest -Uri "${ScriptUrl}?$(Get-Random)" -OutFile $copy -UseBasicParsing + $stale = $false + if ($PSCommandPath -and ($PSCommandPath -ne $copy) -and (Test-Path -LiteralPath $PSCommandPath)) { + Copy-Item -LiteralPath $PSCommandPath -Destination $copy -Force + } elseif (-not (Save-ShortcutCopy $copy)) { + if ($script:ShortcutKept -and -not (Test-Path -LiteralPath $copy)) { [IO.File]::WriteAllBytes($copy, $script:ShortcutKept) } + if (-not (Test-Path -LiteralPath $copy)) { throw 'the download failed. Check the connection and try again.' } + $stale = $true } Unblock-File -LiteralPath $copy -ErrorAction SilentlyContinue if ((Get-ExecutionPolicy -Scope CurrentUser) -in 'Undefined', 'Restricted', 'AllSigned') { @@ -216,6 +240,7 @@ function Switch-Shortcut([switch]$KeepCopy) { if (-not (Test-Path -LiteralPath $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force | Out-Null } Add-Content -LiteralPath $PROFILE "`n$(Get-ShortcutLine)" Say " Shortcut added. In a new PowerShell window, just type: $name" 'Green' + if ($stale) { Say " Could not download the latest version; kept the previous copy." 'Yellow' } } catch { Say " Could not change the shortcut: $($_.Exception.Message)" 'Red' }