# --- 1. Thread & Admin Check (WPF REQUIRES STA-Mode!) --- $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) $isSTA = ([System.Threading.Thread]::CurrentThread.GetApartmentState() -eq 'STA') if (-not $isAdmin -or -not $isSTA) { $argList = "-STA -NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" if (-not $isAdmin) { Start-Process "powershell.exe" -ArgumentList $argList -Verb "RunAs" } else { Start-Process "powershell.exe" -ArgumentList $argList } exit } Add-Type -AssemblyName PresentationFramework Add-Type -Name Window -Namespace Console -MemberDefinition ' [DllImport("Kernel32.dll")] public static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow); ' $consolePtr = [Console.Window]::GetConsoleWindow() [Console.Window]::ShowWindow($consolePtr, 0) # --- 2. Load WPF Assemblies & Setup UTF8 without BOM --- Add-Type -AssemblyName PresentationFramework Add-Type -AssemblyName PresentationCore Add-Type -AssemblyName WindowsBase $utf8NoBom = New-Object System.Text.UTF8Encoding($false) # --- 3. Configuration Paths --- $jobFile = "$PSScriptRoot\backup_jobs.json" $mailFile = "$PSScriptRoot\email_settings.json" if (-not (Test-Path $jobFile)) { [System.IO.File]::WriteAllText($jobFile, "{}", $utf8NoBom) } if (-not (Test-Path $mailFile)) { [System.IO.File]::WriteAllText($mailFile, "{}", $utf8NoBom) } # --- 4. XAML UI Definition --- $xaml = @'