netx/packaging/install_autostart.ps1
oliver e21719b66c Modernize the Windows installer, tray and runtime control; fix lifecycle bugs.
Installer: dynamic light/dark Win11 style, new brand wizard images, auto
language, exclusive run mode (tray / Windows service) plus autostart and
auto-update on the Tasks page (pre-selected from what is installed on
upgrades, untouched by silent updates), 5 localized Start Menu entries.

Tray: theme-aware rounded menu with live status, icons and status-dot tray
icon; health polled off the UI thread; Settings submenu toggles autostart
and daily updates; browser opened de-elevated via explorer.

Fixes:
- tray Stop/Start was ignored in service mode (service_run restarted the
  children) - all start/stop now goes through runtime_ctl.ps1
- logon tray task and SYSTEM service task were killed after 72h (default
  ExecutionTimeLimit); autostart was registered for the elevating account
  and opened a browser on every logon
- uninstall left the NetXTray/NetXUpdate tasks behind
- silent auto-update killed the tray without restarting it
- repair_venv.ps1 was never staged into the release
- wildcard NETX_HOST (0.0.0.0) broke health checks
- ICO frames were PNG, which System.Drawing decodes as noise; icons now
  use the current brand mark

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 20:32:33 +08:00

48 lines
2.3 KiB
PowerShell

param(
[string]$ProgramRoot = "",
[string]$DataRoot = "",
[switch]$Remove = $false
)
# Register / unregister NetX tray at logon with highest privileges (UAC admin).
# HKCU\Run cannot elevate reliably on fresh PCs; use a Scheduled Task instead.
$ErrorActionPreference = "Stop"
. "$PSScriptRoot\_common.ps1"
Assert-NetxAdminOrRelaunch -ScriptPath $PSCommandPath -BoundParameters $PSBoundParameters -WindowStyle Normal
$prog = Get-NetxProgramRoot -Override $ProgramRoot
$data = Get-NetxDataRoot -ProgramRoot $prog -Override $DataRoot
$tray = Join-Path $PSScriptRoot "netx_tray.ps1"
$runKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
$legacyName = "NetX"
$taskName = "NetXTray"
$taskPath = "\NetX\"
# Always clear legacy per-user Run key (non-elevated).
Remove-ItemProperty -Path $runKey -Name $legacyName -ErrorAction SilentlyContinue
if ($Remove) {
Unregister-ScheduledTask -TaskName $taskName -TaskPath $taskPath -Confirm:$false -ErrorAction SilentlyContinue
Write-Host "==> Removed NetX tray autostart task"
exit 0
}
# -QuietStart: bring NetX up at logon without popping a browser window.
$arg = "-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$tray`" -ProgramRoot `"$prog`" -DataRoot `"$data`" -StartOnLaunch -QuietStart"
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $arg -WorkingDirectory $prog
# Any user logon, run for the local Administrators group: Setup runs elevated
# (often as a different admin account), so $env:USERNAME is not the desktop user.
$trig = New-ScheduledTaskTrigger -AtLogOn
$trig.Delay = "PT10S"
$admins = (New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")).Translate(
[System.Security.Principal.NTAccount]).Value
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable `
-ExecutionTimeLimit ([TimeSpan]::Zero) -MultipleInstances Parallel
$principal = New-ScheduledTaskPrincipal -GroupId $admins -RunLevel Highest
Register-ScheduledTask -TaskName $taskName -TaskPath $taskPath `
-Action $action -Trigger $trig -Settings $settings -Principal $principal -Force | Out-Null
Write-Host "==> NetX tray will start at logon (Scheduled Task, RunLevel Highest)" -ForegroundColor Green
Write-Host " $taskPath$taskName"
Write-Host " $arg"