Repository navigation
Expand file tree
/
Copy pathsetup_startup_task.ps1
More file actions
91 lines (76 loc) · 3.95 KB
/
Copy pathsetup_startup_task.ps1
File metadata and controls
91 lines (76 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<#
.SYNOPSIS
Automated registration for the AHK Startup Fleet in Windows Task Scheduler.
.DESCRIPTION
Registers "AHK Startup Script" in Windows Task Scheduler:
- Action: Launches AllScripts\StartupScript.exe
- Trigger: At logon of current user with a configurable delay (default 30 seconds)
- Privilege: Highest available (RunLevel Highest) to enable global keyboard hooks across elevated developer tools (Antigravity IDE, elevated consoles) and standard windows without UIPI isolation
- Resilience: Runs on battery, no execution timeout, demand start allowed
.PARAMETER Uninstall
Unregisters and removes the "AHK Startup Script" task from Windows Task Scheduler.
.PARAMETER DelaySeconds
Delay in seconds after user logon before launching StartupScript.exe (default: 30).
#>
param(
[switch]$Uninstall,
[int]$DelaySeconds = 30
)
$ErrorActionPreference = "Stop"
$taskName = "AHK Startup Script"
# Check for Administrator privileges
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "Requesting Administrator elevation to register Scheduled Task..." -ForegroundColor Yellow
$argsList = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
if ($Uninstall) { $argsList += " -Uninstall" }
if ($DelaySeconds -ne 30) { $argsList += " -DelaySeconds $DelaySeconds" }
Start-Process powershell.exe -ArgumentList $argsList -Verb RunAs -Wait
exit
}
if ($Uninstall) {
$existing = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($existing) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
Write-Host "[OK] Unregistered scheduled task: $taskName" -ForegroundColor Yellow
} else {
Write-Host "[INFO] Task '$taskName' is not registered." -ForegroundColor Gray
}
exit 0
}
$root = $PSScriptRoot
$workDir = Join-Path $root "AllScripts"
$exePath = Join-Path $workDir "StartupScript.exe"
if (-not (Test-Path $exePath)) {
Write-Host "[WARNING] StartupScript.exe not found at $exePath" -ForegroundColor Yellow
Write-Host "Please compile it first by running: .\build_startup_exe.ps1" -ForegroundColor Yellow
Write-Host "Registering task anyway so it will be ready once compiled..." -ForegroundColor Gray
}
# 1. Action: Execute compiled StartupScript.exe with AllScripts as working directory
$action = New-ScheduledTaskAction -Execute $exePath -WorkingDirectory $workDir
# 2. Trigger: At logon of current user, delayed by N seconds to let Windows settle
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
if ($DelaySeconds -gt 0) {
$trigger.Delay = "PT$($DelaySeconds)S"
}
# 3. Principal: Elevated interactive user session to allow global hooks into elevated developer windows
$principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive -RunLevel Highest
# 4. Settings: Run on battery, allow manual start, and remove default execution timeout
$settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-ExecutionTimeLimit ([TimeSpan]::Zero)
# 5. Register or overwrite task
Register-ScheduledTask -TaskName $taskName `
-Action $action `
-Trigger $trigger `
-Principal $principal `
-Settings $settings `
-Force | Out-Null
Write-Host "========================================================" -ForegroundColor Green
Write-Host " [OK] Successfully registered task: '$taskName'" -ForegroundColor Green
Write-Host "========================================================" -ForegroundColor Green
Write-Host "Target: $exePath" -ForegroundColor White
Write-Host "Trigger: At logon of $env:USERNAME ($DelaySeconds-second delay)" -ForegroundColor White
Write-Host "To test: Start-ScheduledTask -TaskName '$taskName'" -ForegroundColor Gray
Write-Host "To remove: .\setup_startup_task.ps1 -Uninstall" -ForegroundColor Gray