{"record":{"id":"bd3e5ff31f9608ad","repo":"itsfatduck/optimizerDuck","slug":"scheduled-task-verify-failed-at-fullpath-could-not-query-the","errorCode":null,"errorMessage":"Scheduled task verify failed at {FullPath}: could not query the task state.","messagePattern":"Scheduled task verify failed at (.+?): could not query the task state\\.","errorType":"exception","errorClass":"StepExecutionException","httpStatus":null,"severity":"error","filePath":"optimizerDuck/Domain/Revert/Steps/ScheduledTaskRevertStep.cs","lineNumber":52,"sourceCode":"            : Loc.Instance[\"Revert.ScheduledTask.Description.Disable\", FullPath];\n\n    /// <inheritdoc />\n    public Task<bool> ExecuteAsync(ShellService _, ILogger logger)\n    {\n        var opCall = new OpCall { Logger = logger };\n        var result = OriginalEnabled\n            ? ScheduledTaskService.EnableTask(opCall, FullPath)\n            : ScheduledTaskService.DisableTask(opCall, FullPath);\n\n        if (!result.Ok)\n            throw new StepExecutionException(result.Error ?? Description, result.ErrorDetail);\n\n        // a missing task has nothing to restore; anything else must verify positively.\n        var state = ScheduledTaskService.GetTaskEnabledState(FullPath, opCall.Logger);\n        if (state is TaskEnabledState.NotFound)\n            return Task.FromResult(true);\n        if (state is not (TaskEnabledState.Enabled or TaskEnabledState.Disabled))\n            throw new StepExecutionException(\n                $\"Scheduled task verify failed at {FullPath}: could not query the task state.\",\n                null\n            );\n        if ((state == TaskEnabledState.Enabled) != OriginalEnabled)\n            throw new StepExecutionException(\n                $\"Scheduled task verify failed at {FullPath}: expected enabled={OriginalEnabled}, actual={state == TaskEnabledState.Enabled}\",\n                null\n            );\n\n        return Task.FromResult(true);\n    }\n\n    /// <inheritdoc />\n    public JObject ToData()\n    {\n        return new JObject\n        {\n            [nameof(FullPath)] = FullPath,","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/itsfatduck/optimizerDuck/blob/36acf585ae122a09f0e29eb4acedfaf94e81c0de/optimizerDuck/Domain/Revert/Steps/ScheduledTaskRevertStep.cs#L34-L70","documentation":"ScheduledTaskRevertStep.ExecuteAsync throws this when, after attempting to restore a scheduled task's enabled state, the re-query via ScheduledTaskService.GetTaskEnabledState returns Unknown (not Enabled, Disabled, or NotFound). The step refuses to report success without positive verification: a missing task is treated as restored, but a task whose state cannot be read is a failure.","triggerScenarios":"Reverting a scheduled task where the Task Scheduler query fails — e.g. the Task Scheduler service is stopped or corrupted, the task path is malformed so the query errors instead of returning NotFound, or an access/COM error occurs during ts.GetTask. GetTaskEnabledState then returns TaskEnabledState.Unknown, which hits this throw.","commonSituations":"Task Scheduler service (Schedule) disabled by the very optimization being reverted; registry corruption in HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Schedule; task deleted between apply and revert but the query still fails rather than reporting NotFound; permissions issues after a Windows upgrade.","solutions":["Ensure the Task Scheduler service is running (sc query Schedule; net start Schedule) and retry the revert.","Verify the FullPath stored in the revert JSON is a valid task path (\\Folder\\TaskName) and that the task still exists via schtasks /query /tn <path>.","Re-run the revert as administrator — non-elevated queries can fail instead of returning a clean state.","If the task is genuinely gone, delete the revert file %LocalAppData%\\optimizerDuck\\Revert\\{id}.json or accept the step failure since nothing needs restoring."],"exampleFix":"// before\nts.GetTask(fullPath) // may throw or fail when Task Scheduler service is stopped\n// after\n// start the service first, then query\nShellService CMD: \"net start Schedule\"; then retry ScheduledTaskRevertStep.ExecuteAsync","handlingStrategy":"try-catch","validationCode":"// before revert\nvar state = ScheduledTaskService.GetTaskEnabledState(fullPath, logger);\nif (state is not (TaskEnabledState.Enabled or TaskEnabledState.Disabled or TaskEnabledState.NotFound))\n    throw new InvalidOperationException(\"Task Scheduler unavailable; fix service before reverting\");","typeGuard":"static bool IsQueryable(TaskEnabledState s) => s is TaskEnabledState.Enabled or TaskEnabledState.Disabled or TaskEnabledState.NotFound;","tryCatchPattern":"try { await step.ExecuteAsync(opCall); }\ncatch (StepExecutionException ex) when (ex.Message.Contains(\"could not query the task state\"))\n{ logger.LogWarning(ex, \"Task Scheduler query failed; ensure Schedule service is running, then retry\"); }","preventionTips":["Keep the Task Scheduler service running; do not optimize it away","Always revert while elevated","Validate task paths with schtasks /query before reverting","Persist FullPath exactly as returned by GetAllTasks"],"tags":["csharp","scheduled-tasks","windows","revert","verification"],"backgroundTag":"internal-invariant-violation","analyzedSha":"36acf585ae122a09f0e29eb4acedfaf94e81c0de","analyzedAt":"2026-09-13T08:30:13.845Z","contentChangedAt":"2026-09-13T08:30:13.845Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}