Launch.ps1

# Started by "Start Scope Tag Manager.cmd". Opens the tool in the default browser; sign-in happens on the page.
# Runs without a console window, so any start-up problem is shown in a message box and written to launcher.log.
$ErrorActionPreference = 'Stop'
$base = if ($env:LOCALAPPDATA) { $env:LOCALAPPDATA } else { Join-Path $HOME '.local/share' }
$logDir = Join-Path $base 'IntuneScopeTagManager'
$log = Join-Path $logDir 'launcher.log'
function Write-LaunchLog { param([string]$Text) try { if (-not (Test-Path -LiteralPath $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }; Add-Content -LiteralPath $log -Value "$([DateTime]::UtcNow.ToString('o')) $Text" -Encoding utf8 } catch { } }
try {
    Write-LaunchLog "Starting from $PSScriptRoot with PowerShell $($PSVersionTable.PSVersion)"
    # Files from a downloaded zip carry the internet mark; clear it on this copy so Windows PowerShell loads them.
    try { Get-ChildItem -LiteralPath $PSScriptRoot -Recurse -File | Unblock-File -ErrorAction SilentlyContinue } catch { }
    Import-Module (Join-Path $PSScriptRoot 'IntuneScopeTagManager.psd1') -Force
    Start-ScopeTagManager -Launcher | Out-Null
    Write-LaunchLog 'Stopped normally'
}
catch {
    $msg = $_.Exception.Message
    Write-LaunchLog "Failed: $msg"
    $text = "Intune Scope Tag Manager could not start.`n`n$msg`n`nDetails: $log"
    try { Add-Type -AssemblyName System.Windows.Forms; [void][System.Windows.Forms.MessageBox]::Show($text, 'Intune Scope Tag Manager', 'OK', 'Error') }
    catch { Write-Host $text }
    exit 1
}