[Console]::OutputEncoding = [Text.Encoding]::UTF8 $ErrorActionPreference = 'Stop' function Test-Installed { $keys = @( 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*', 'HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' ) Get-ItemProperty $keys -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -match 'Thunderbird' } | Select-Object -First 1 DisplayName,DisplayVersion,InstallLocation } $existing = Test-Installed if ($existing) { Write-Host "[=] Thunderbird already installed: $($existing.DisplayName) $($existing.DisplayVersion)" Write-Host " Location: $($existing.InstallLocation)" exit 0 } $installed = $false # --- Attempt 1: winget --- $winget = Get-Command winget -ErrorAction SilentlyContinue if ($winget) { Write-Host "[*] winget found, installing Mozilla.Thunderbird" try { & winget install --id Mozilla.Thunderbird -e --silent ` --accept-source-agreements --accept-package-agreements ` --scope machine 2>&1 | Write-Host if ($LASTEXITCODE -eq 0) { $installed = $true } else { Write-Host "[!] winget exited $LASTEXITCODE, falling back to direct download" } } catch { Write-Host "[!] winget failed: $_" } } else { Write-Host "[!] winget not available, using direct download" } # --- Attempt 2: direct download from Mozilla CDN --- if (-not $installed) { $url = 'https://download.mozilla.org/?product=thunderbird-latest&os=win64&lang=zh-TW' $dst = Join-Path $env:TEMP 'thunderbird-setup.exe' Write-Host "[*] Downloading installer -> $dst" [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri $url -OutFile $dst -UseBasicParsing if (-not (Test-Path $dst) -or (Get-Item $dst).Length -lt 10MB) { throw "Downloaded file looks wrong (size < 10 MB)" } Write-Host "[*] Running silent install" $proc = Start-Process -FilePath $dst -ArgumentList '/S' -Wait -PassThru if ($proc.ExitCode -ne 0) { throw "Installer exited with code $($proc.ExitCode)" } Remove-Item $dst -ErrorAction SilentlyContinue $installed = $true } # --- Verify --- Start-Sleep -Seconds 2 $after = Test-Installed if ($after) { Write-Host "" Write-Host "[OK] Installed: $($after.DisplayName) $($after.DisplayVersion)" Write-Host " Location: $($after.InstallLocation)" exit 0 } else { Write-Host "[FAIL] Thunderbird was not detected in uninstall registry after install" exit 1 }