{"record":{"id":"13262cf0ec58e447","repo":"chenhg5/cc-connect","slug":"register-scheduled-task-s-w","errorCode":null,"errorMessage":"register scheduled task: %s (%w)","messagePattern":"register scheduled task: (.+?) \\(%w\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"daemon/windows.go","lineNumber":158,"sourceCode":"}\n\nfunc windowsTaskAction(scriptPath string) string {\n\treturn fmt.Sprintf(`powershell.exe %s`, windowsTaskActionArgs(scriptPath))\n}\n\nfunc windowsTaskActionArgs(scriptPath string) string {\n\treturn fmt.Sprintf(`-WindowStyle Hidden -NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"%s\"`, scriptPath)\n}\n\nfunc createWindowsTask(scriptPath string) error {\n\tout, err := runPowerShell(fmt.Sprintf(`\n$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument %s\n$trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME\n$principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive -RunLevel Limited\nRegister-ScheduledTask -TaskName %s -Action $action -Trigger $trigger -Principal $principal -Force | Out-Null\n`, powerShellLiteral(windowsTaskActionArgs(scriptPath)), powerShellLiteral(windowsTaskName)))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"register scheduled task: %s (%w)\", out, err)\n\t}\n\treturn nil\n}\n\nfunc windowsTaskMatchesAction(scriptPath string) bool {\n\tout, err := runPowerShell(fmt.Sprintf(`\n$task = Get-ScheduledTask -TaskName %s -ErrorAction SilentlyContinue\nif ($null -eq $task) { exit 1 }\n$expectedArgs = %s\nforeach ($action in $task.Actions) {\n\tif (($action.Execute -ieq 'powershell.exe') -and ($action.Arguments -eq $expectedArgs)) {\n\t\tWrite-Output 'true'\n\t\texit 0\n\t}\n}\nexit 1\n`, powerShellLiteral(windowsTaskName), powerShellLiteral(windowsTaskActionArgs(scriptPath))))\n\treturn err == nil && strings.EqualFold(strings.TrimSpace(out), \"true\")","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/daemon/windows.go#L140-L176","documentation":"createWindowsTask wraps an error from runPowerShell executing a Register-ScheduledTask script. The message includes the captured stdout/stderr (out) plus the wrapped error, indicating the scheduled task could not be registered with Task Scheduler. %s carries the PowerShell output, %w the exec error.","triggerScenarios":"daemon.Install on Windows when the embedded PowerShell (New-ScheduledTaskAction / New-ScheduledTaskTrigger -AtLogOn / New-ScheduledTaskPrincipal -LogonType Interactive -RunLevel Limited / Register-ScheduledTask -Force) fails: powershell.exe not on PATH, exec error, or Register-ScheduledTask returning an error (access denied, invalid principal, policy).","commonSituations":"PowerShell missing or PATH broken on minimal Windows installs; running as SYSTEM/service account where Interactive logon principal is invalid; group policy restricting scheduled task registration; running in a non-interactive session.","solutions":["Read the %s output in the error — it contains the exact PowerShell failure message.","Verify powershell.exe is available: `powershell.exe -NoProfile -Command \"echo ok\"`.","Run the install from an interactive user session (the principal requires an interactive logon user), not from a service or SYSTEM context.","Register the task manually with the same command in a PowerShell window to see the full error.","Check group policy / antivirus restrictions on Register-ScheduledTask."],"exampleFix":"// reproduce manually to see the real reason\npowershell -NoProfile -Command \"Register-ScheduledTask -TaskName cc-connect -Action (New-ScheduledTaskAction -Execute powershell.exe -Argument '-File script.ps1') -Trigger (New-ScheduledTaskTrigger -AtLogOn) -Principal (New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive -RunLevel Limited) -Force\"","handlingStrategy":"validation","validationCode":"if _, err := exec.LookPath(\"powershell\"); err != nil {\n\treturn fmt.Errorf(\"powershell.exe not found on PATH\")\n}","typeGuard":null,"tryCatchPattern":"if err := daemon.Install(scriptPath); err != nil {\n\tvar msg string\n\tif strings.Contains(err.Error(), \"register scheduled task\") {\n\t\tfmt.Sscanf(err.Error(), \"register scheduled task: %s\", &msg) // captured PowerShell output\n\t}\n\tslog.Error(\"task registration failed; run install from an interactive session\", \"error\", err)\n}","preventionTips":["Run installs from a normal interactive user session (the principal requires Interactive logon).","Verify powershell.exe is on PATH before automating installs.","Check group policy permits Register-ScheduledTask on the machine."],"tags":["windows","scheduled-task","powershell","registration"],"backgroundTag":"http-request-failed","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}