[CmdletBinding()] param( [string]$Version, [switch]$NoStartup, [switch]$NoLaunch ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest if ($Version -and $Version -notmatch '^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$') { throw "Invalid PanePilot version: $Version" } $repository = 'murayy/PanePilot' $installRoot = Join-Path $env:LOCALAPPDATA 'Programs\PanePilot' $startMenuRoot = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs\PanePilot' $startupShortcut = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs\Startup\PanePilot.lnk' $temporaryRoot = Join-Path ([IO.Path]::GetTempPath()) ("PanePilot-Install-{0}" -f [guid]::NewGuid()) function New-PanePilotShortcut { param( [Parameter(Mandatory)] [string]$Path, [Parameter(Mandatory)] [string]$TargetPath, [string]$Arguments = '' ) $parent = Split-Path -Parent $Path [IO.Directory]::CreateDirectory($parent) | Out-Null $shell = New-Object -ComObject WScript.Shell $shortcut = $shell.CreateShortcut($Path) $shortcut.TargetPath = $TargetPath $shortcut.Arguments = $Arguments $shortcut.WorkingDirectory = Split-Path -Parent $TargetPath $shortcut.IconLocation = "$TargetPath,0" $shortcut.Save() } function Stop-InstalledPanePilot { param([Parameter(Mandatory)] [string]$ExecutablePath) $expected = [IO.Path]::GetFullPath($ExecutablePath) Get-CimInstance Win32_Process -Filter "Name = 'PanePilot.exe'" -ErrorAction SilentlyContinue | Where-Object { $_.ExecutablePath -and ([IO.Path]::GetFullPath($_.ExecutablePath) -eq $expected) } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue } } try { [IO.Directory]::CreateDirectory($temporaryRoot) | Out-Null $releaseUri = if ($Version) { "https://api.github.com/repos/$repository/releases/tags/v$Version" } else { "https://api.github.com/repos/$repository/releases/latest" } Write-Host 'Resolving PanePilot release...' $release = Invoke-RestMethod -Uri $releaseUri -Headers @{ 'User-Agent' = 'PanePilot-Installer' } $resolvedVersion = [string]$release.tag_name -replace '^v', '' $archiveName = "PanePilot-v$resolvedVersion-win-x64.zip" $archiveAsset = $release.assets | Where-Object name -EQ $archiveName | Select-Object -First 1 $checksumAsset = $release.assets | Where-Object name -EQ 'SHA256SUMS.txt' | Select-Object -First 1 if (-not $archiveAsset -or -not $checksumAsset) { throw "Release v$resolvedVersion does not contain $archiveName and SHA256SUMS.txt." } $archivePath = Join-Path $temporaryRoot $archiveName $checksumPath = Join-Path $temporaryRoot 'SHA256SUMS.txt' Invoke-WebRequest -UseBasicParsing -Uri $archiveAsset.browser_download_url -OutFile $archivePath Invoke-WebRequest -UseBasicParsing -Uri $checksumAsset.browser_download_url -OutFile $checksumPath $checksumLine = Get-Content -LiteralPath $checksumPath | Where-Object { $_ -match "^[A-Fa-f0-9]{64}\s+\*?$([regex]::Escape($archiveName))$" } | Select-Object -First 1 if (-not $checksumLine) { throw "No checksum was published for $archiveName." } $expectedHash = ($checksumLine -split '\s+')[0].ToUpperInvariant() $actualHash = (Get-FileHash -LiteralPath $archivePath -Algorithm SHA256).Hash.ToUpperInvariant() if ($actualHash -ne $expectedHash) { throw "Checksum mismatch for $archiveName. Expected $expectedHash, got $actualHash." } $expandedRoot = Join-Path $temporaryRoot 'expanded' Expand-Archive -LiteralPath $archivePath -DestinationPath $expandedRoot $payloadRoot = Get-ChildItem -LiteralPath $expandedRoot -Directory | Select-Object -First 1 if (-not $payloadRoot -or -not (Test-Path -LiteralPath (Join-Path $payloadRoot.FullName 'PanePilot.exe'))) { throw 'The release archive does not contain PanePilot.exe.' } $installedExe = Join-Path $installRoot 'PanePilot.exe' Stop-InstalledPanePilot -ExecutablePath $installedExe if (Test-Path -LiteralPath $installRoot) { $expectedInstallRoot = [IO.Path]::GetFullPath((Join-Path $env:LOCALAPPDATA 'Programs\PanePilot')) if ([IO.Path]::GetFullPath($installRoot) -ne $expectedInstallRoot) { throw "Refusing to replace unexpected path: $installRoot" } Remove-Item -LiteralPath $installRoot -Recurse -Force } [IO.Directory]::CreateDirectory($installRoot) | Out-Null Copy-Item -Path (Join-Path $payloadRoot.FullName '*') -Destination $installRoot -Recurse -Force New-PanePilotShortcut -Path (Join-Path $startMenuRoot 'PanePilot.lnk') -TargetPath $installedExe $uninstaller = Join-Path $installRoot 'Uninstall.ps1' New-PanePilotShortcut -Path (Join-Path $startMenuRoot 'Uninstall PanePilot.lnk') ` -TargetPath (Get-Command powershell.exe).Source ` -Arguments "-NoProfile -ExecutionPolicy Bypass -File `"$uninstaller`"" if ($NoStartup) { Remove-Item -LiteralPath $startupShortcut -Force -ErrorAction SilentlyContinue } else { New-PanePilotShortcut -Path $startupShortcut -TargetPath $installedExe } if (-not $NoLaunch) { Start-Process -FilePath $installedExe } Write-Host "PanePilot v$resolvedVersion installed to $installRoot" -ForegroundColor Green Write-Host $(if ($NoStartup) { 'Windows startup is disabled.' } else { 'Windows startup is enabled.' }) } finally { if (Test-Path -LiteralPath $temporaryRoot) { $safeTempPrefix = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()).TrimEnd('\') + '\' $resolvedTemporaryRoot = [IO.Path]::GetFullPath($temporaryRoot) if ($resolvedTemporaryRoot.StartsWith($safeTempPrefix, [StringComparison]::OrdinalIgnoreCase)) { Remove-Item -LiteralPath $temporaryRoot -Recurse -Force -ErrorAction SilentlyContinue } } }