boot-task-install.template.ps1 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <#
  2. .SYNOPSIS
  3. TEMPLATE: Register a Windows Task Scheduler entry to launch Process Compose at boot.
  4. .DESCRIPTION
  5. Copy to <your-stack>/scripts/boot-task-install.ps1, customise $taskName, and
  6. run as Administrator.
  7. Pairs with boot-start.template.ps1 (the wrapper script that this task launches).
  8. Uses LogonType S4U so the task runs at boot without storing a password or
  9. requiring interactive logon.
  10. .EXAMPLE
  11. # In elevated PowerShell:
  12. .\boot-task-install.ps1
  13. #>
  14. [CmdletBinding()]
  15. param()
  16. $ErrorActionPreference = 'Stop'
  17. # Admin check
  18. $currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
  19. $principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
  20. if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  21. throw "Run as Administrator. Task Scheduler creation requires it."
  22. }
  23. $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  24. $root = (Resolve-Path (Join-Path $scriptDir '..')).Path
  25. $bootScript = Join-Path $scriptDir 'boot-start.ps1'
  26. if (-not (Test-Path $bootScript)) {
  27. throw "boot-start.ps1 not found at $bootScript - copy boot-start.template.ps1 and customise"
  28. }
  29. # ─── CUSTOMIZE TASK NAME ──────────────────────────────────────────────────
  30. $taskName = "ProcessCompose-MyStack" # rename per project
  31. Write-Host "Registering Task Scheduler entry: $taskName" -ForegroundColor Cyan
  32. # Idempotent: remove existing if present
  33. $existing = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
  34. if ($existing) {
  35. Write-Host " Removing existing task..."
  36. Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
  37. }
  38. # Launch via PowerShell, hidden window, running boot-start.ps1
  39. $action = New-ScheduledTaskAction `
  40. -Execute "powershell.exe" `
  41. -Argument "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$bootScript`"" `
  42. -WorkingDirectory $root
  43. $trigger = New-ScheduledTaskTrigger -AtStartup
  44. $settings = New-ScheduledTaskSettingsSet `
  45. -ExecutionTimeLimit (New-TimeSpan -Seconds 0) `
  46. -AllowStartIfOnBatteries `
  47. -DontStopIfGoingOnBatteries `
  48. -RestartCount 3 `
  49. -RestartInterval (New-TimeSpan -Minutes 1)
  50. # S4U: runs at boot as current user, no password stored, no interactive logon needed.
  51. # Sufficient for local services with no GUI or network-share requirements.
  52. $taskPrincipal = New-ScheduledTaskPrincipal `
  53. -UserId $env:USERNAME `
  54. -LogonType S4U `
  55. -RunLevel Highest
  56. Register-ScheduledTask `
  57. -TaskName $taskName `
  58. -Action $action `
  59. -Trigger $trigger `
  60. -Settings $settings `
  61. -Principal $taskPrincipal `
  62. -Description "Starts Process Compose at boot."
  63. Write-Host ""
  64. Write-Host "Done. Task registered." -ForegroundColor Green
  65. Write-Host ""
  66. Write-Host "Verify:"
  67. Write-Host " Get-ScheduledTask -TaskName '$taskName'"
  68. Write-Host ""
  69. Write-Host "Test before reboot:"
  70. Write-Host " Start-ScheduledTask -TaskName '$taskName'"
  71. Write-Host " Start-Sleep 10"
  72. Write-Host " process-compose -p 8888 process list"
  73. Write-Host ""
  74. Write-Host "Uninstall:"
  75. Write-Host " Unregister-ScheduledTask -TaskName '$taskName' -Confirm:`$false"