<# .SYNOPSIS PrinterFix Utility — Main entry point. Diagnoses and repairs printer issues on Windows 10/11 via a WinForms GUI. .DESCRIPTION This script is the single entry point. It loads the library scripts (Common, Diagnostics, Repairs, GUI) from the lib/ subfolder and launches the GUI. This file is host-agnostic: upload the SAME copy to any web server. The location of lib/ is passed in at run time - never edited in - via the PFX_RemoteRoot environment variable on the calling line: powershell -NoProfile -Command " `$env:PFX_RemoteRoot = 'https://printer.thesunrisecomputers.com'; irm https://printer.thesunrisecomputers.com | iex" (iex in 5.1 takes no arguments, so the root is handed to the script through the process env var instead. The script falls back to the PFX_RemoteRoot param, so baking it in still works too.) On-disk / side-by-side runs still work: powershell -ExecutionPolicy Bypass -File .\PrinterFix.ps1 Diagnostics run without admin rights. Repair actions auto-elevate via UAC when required. .NOTES Target : Windows 10/11 Home & Pro PS : 5.1+ Version: 1.0 #> [CmdletBinding()] param( [switch]$Elevated, [string]$LogFileOverride = "", # Piped (irm | iex) mode only: the URL of the folder on your web server # that holds PrinterFix.ps1 + lib/, e.g. "https://yoursite.com/PrinterFix". # Because 5.1's iex takes no arguments, the usual way to supply it is the # PFX_RemoteRoot environment variable on the calling line (resolved below # when this param is empty). Baking a value here still works as an # override for a fixed deployment - but the file you ship can stay "". [string]$PFX_RemoteRoot = "", # Deployment smoke test: verify download/load/config end-to-end # WITHOUT launching the (blocking) GUI. Use it to check your web root # before handing out the one-liner (5.1's iex takes no extra arguments, # so download first, then run with -SelfTest): # powershell -NoProfile -Command "irm http://yoursite/PrinterFix.ps1 -OutFile $env:TEMP\pfx.ps1; & $env:TEMP\pfx.ps1 -SelfTest -PFX_RemoteRoot http://yoursite/" [switch]$SelfTest ) # ── Resolve the remote root for piped mode ─────────────────────────────────── # iex can't forward arguments in 5.1, so the caller passes the web root as the # PFX_RemoteRoot environment variable. If it was NOT passed as a param and the # env var is set, use it. A param value (or a baked-in default) always wins. if (-not $PFX_RemoteRoot -and $env:PFX_RemoteRoot) { $PFX_RemoteRoot = $env:PFX_RemoteRoot } # ── Library list + base directory resolution ──────────────────────────────── $libFiles = @( "Common.ps1", "Diagnostics.ps1", "Repairs.ps1", "GUI.ps1" ) # ── Download helper (shared by piped mode and -SelfTest) ──────────────────── # Fetches lib/*.ps1 + the entry script from $Root into $StageDir. Persisting # the entry script is what lets the UAC re-launch (powershell -File ... # -Elevated) find a real file on disk. function Start-PfxScriptStaging { param( [Parameter(Mandatory)][string]$Root, [Parameter(Mandatory)][string]$StageDir ) $r = $Root.TrimEnd('/') $libDir = Join-Path $StageDir "lib" if (-not (Test-Path $libDir)) { New-Item -Path $libDir -ItemType Directory -Force | Out-Null } # Some Win10 builds default to TLS 1.0/1.1 for .NET web requests; force 1.2. [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 foreach ($name in $libFiles) { $url = "$r/lib/$name" $dest = Join-Path $libDir $name Write-Host " GET $url" -ForegroundColor DarkGray Invoke-WebRequest -Uri $url -OutFile $dest -UseBasicParsing } $entryUrl = "$r/PrinterFix.ps1" $entryDest = Join-Path $StageDir "PrinterFix.ps1" Write-Host " GET $entryUrl" -ForegroundColor DarkGray Invoke-WebRequest -Uri $entryUrl -OutFile $entryDest -UseBasicParsing } # A piped script (irm | iex) has no file path: PSCommandPath/PSScriptRoot # are both empty. Detect that, and stage lib/ + the entry script locally # by downloading them from $PFX_RemoteRoot. $isPiped = [string]::IsNullOrWhiteSpace($PSCommandPath) -and [string]::IsNullOrWhiteSpace($PSScriptRoot) $scriptBase = if ($isPiped) { Join-Path $env:TEMP "PrinterFix" } else { Split-Path -Parent $PSCommandPath } $libDir = Join-Path $scriptBase "lib" if ($isPiped) { if (-not $PFX_RemoteRoot) { Write-Host "" Write-Host "ERROR: piped mode (irm | iex) needs the web root that holds lib/." -ForegroundColor Red Write-Host " Pass it on the calling line via the PFX_RemoteRoot env var, e.g.:" -ForegroundColor Red Write-Host ' powershell -NoProfile -Command "' -ForegroundColor Red Write-Host " `$env:PFX_RemoteRoot = 'https://yoursite.com/PrinterFix';" -ForegroundColor Red Write-Host " irm https://yoursite.com/PrinterFix/PrinterFix.ps1 | iex" -ForegroundColor Red Write-Host ' "' -ForegroundColor Red Write-Host " (Or bake a default into the hosted copy's PFX_RemoteRoot param.)" -ForegroundColor Red exit 1 } Write-Host "" Write-Host " Piped mode: staging scripts under $scriptBase" -ForegroundColor DarkGray try { Start-PfxScriptStaging -Root $PFX_RemoteRoot -StageDir $scriptBase } catch { Write-Host "ERROR: could not download supporting scripts: $($_.Exception.Message)" -ForegroundColor Red Write-Host " Check that $($PFX_RemoteRoot.TrimEnd('/'))/lib/ is reachable with valid TLS" Write-Host " (or plain HTTP on an intranet)." exit 1 } } # ── SelfTest: verify the web root end-to-end WITHOUT launching the GUI ────── if ($SelfTest) { if (-not $PFX_RemoteRoot) { Write-Host "ERROR: -SelfTest requires -PFX_RemoteRoot (your web root URL)." -ForegroundColor Red exit 1 } $stageDir = Join-Path $env:TEMP "PrinterFix_SelfTest" Write-Host "" Write-Host " SelfTest: verifying self-hosted root $($PFX_RemoteRoot.TrimEnd('/')) ..." -ForegroundColor DarkGray try { Start-PfxScriptStaging -Root $PFX_RemoteRoot -StageDir $stageDir foreach ($name in $libFiles) { . (Join-Path $stageDir "lib/$name") } # The critical symbols must be defined after loading. $missing = @() foreach ($sym in @("Preload-Assemblies","Initialize-ScriptConfig","Build-GUI", "Test-PrintSpoolerService","Repair-PrintSpooler","Write-AppLog")) { if (-not (Get-Command $sym -ErrorAction SilentlyContinue)) { $missing += $sym } } if ($missing) { throw "Downloaded lib/ is missing these symbols: $($missing -join '; ')" } Preload-Assemblies $ScriptConfig = Initialize-ScriptConfig -LogFileOverride $LogFileOverride New-LogFile Write-Host " SelfTest: lib/ downloaded, loaded, and initialized OK." -ForegroundColor Green } catch { Write-Host " SelfTest FAILED: $($_.Exception.Message)" -ForegroundColor Red exit 1 } Write-Host " SelfTest PASS - safe to hand out: irm $($PFX_RemoteRoot.TrimEnd('/'))/PrinterFix.ps1 | iex" -ForegroundColor Green exit 0 } # ── Load library scripts (dot-source directly to import functions into scope) ─ $loadedAll = $true foreach ($name in $libFiles) { $path = Join-Path $libDir $name if (-not (Test-Path $path)) { Write-Warning "Library not found: $path" $loadedAll = $false continue } Write-Host " Loading: $name" -ForegroundColor DarkGray . $path } if (-not $loadedAll) { Write-Error "Failed to load one or more library scripts. Verify that the lib/ folder exists alongside PrinterFix.ps1." Write-Error "If running via 'irm | iex', ensure the lib/ files are also hosted on the same web server." exit 1 } # ── Initialise ─────────────────────────────────────────────────────────────── Preload-Assemblies $ScriptConfig = Initialize-ScriptConfig -LogFileOverride $LogFileOverride # Start-ProcessElevated (lib/Common.ps1) needs the real entry path to # re-launch via UAC. In on-disk mode that's $PSCommandPath; in piped # mode the script was persisted to $scriptBase\PrinterFix.ps1 above, # which is exactly the TEMP fallback Start-ProcessElevated already uses. if ($PSCommandPath) { $ScriptConfig.EntryScriptPath = $PSCommandPath } New-LogFile # If called with -Elevated flag, re-detect admin status if ($Elevated) { $ScriptConfig.IsAdmin = (Test-IsAdmin) Write-AppLog "Started in elevated mode. Admin: $($ScriptConfig.IsAdmin)" } # ── Console banner ─────────────────────────────────────────────────────────── Write-Host "" Write-Host " PrinterFix Utility v$($ScriptConfig.AppVersion)" -ForegroundColor Cyan Write-Host " Computer : $env:COMPUTERNAME" Write-Host " User : $env:USERNAME" Write-Host " Admin : $((Test-IsAdmin))" Write-Host " Log : $($ScriptConfig.LogFile)" Write-Host "" # ── Launch GUI ─────────────────────────────────────────────────────────────── try { Build-GUI } catch { $errMsg = $_.Exception.Message Write-Host "ERROR: $errMsg" -ForegroundColor Red Write-AppLog "GUI failed to launch: $errMsg" "ERROR" Write-Host "" Write-Host " The log file at $($ScriptConfig.LogFile) contains full details." -ForegroundColor Yellow exit 1 }