#Requires -RunAsAdministrator
<#
  Installs a scheduled task: at every logon, opens a console with Show-LoginDashboard.ps1
  Run this script once as Administrator (right-click → Run with PowerShell as admin).
#>
$ErrorActionPreference = 'Stop'
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$scriptPath = Join-Path $here 'Show-LoginDashboard.ps1'
if (-not (Test-Path $scriptPath)) {
    Write-Error "Missing: $scriptPath"
    exit 1
}

$taskName = 'LoginSiteDashboard'
$ps = Join-Path $env:WINDIR 'System32\WindowsPowerShell\v1.0\powershell.exe'
$args = "-NoProfile -ExecutionPolicy Bypass -NoExit -File `"$scriptPath`""

Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue

$action = New-ScheduledTaskAction -Execute $ps -Argument $args
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
$principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive -RunLevel Limited

Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -Principal $principal | Out-Null
Write-Host "OK: Task '$taskName' registered for user $env:USERNAME at logon." -ForegroundColor Green
Write-Host "Log off and back on, or run: Start-ScheduledTask -TaskName '$taskName'" -ForegroundColor Cyan
