public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { var taskName = request.DataStore.GetValue("TaskName"); TaskCollection tasks = null; using (TaskService ts = new TaskService()) { tasks = ts.RootFolder.GetTasks(new Regex(taskName)); foreach (Task task in tasks) { try { RunningTask runningTask = task.Run(); return(new ActionResponse(ActionStatus.Success, JsonUtility.GetEmptyJObject())); } catch (Exception e) { if (NTHelper.IsCredentialGuardEnabled()) { return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "CredentialGuardEnabled")); } return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), e, "RunningTaskFailed")); } } } // We should never return this return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "SccmTaskNotFound")); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { const int MAX_RETRIES = 15; // This loop tries to avoid an unknown task state, thinking it's transient Task task = null; for (int i = 0; i <= MAX_RETRIES; i++) { System.Threading.Thread.Sleep(100); task = GetScheduledTask(request.DataStore.GetValue("TaskName")); if (task == null) { return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "SccmTaskNotFound")); } if (task.State == TaskState.Unknown) { continue; } else { break; } } // Let it run if (task.State == TaskState.Queued || task.State == TaskState.Running) { return(new ActionResponse(ActionStatus.InProgress)); } // If we're here, the task completed if (task.LastTaskResult == 0) { return(new ActionResponse(ActionStatus.Success)); } // there was an error since we haven't exited above uploadLogs(request); if (NTHelper.IsCredentialGuardEnabled()) { return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "CredentialGuardEnabled")); } // azurebcp exits with 0, 1, 2, the powershell script might return 1 - anything else must be a Windows error Exception e = (uint)task.LastTaskResult > 2 ? new Exception($"Scheduled task exited with code {task.LastTaskResult}", new System.ComponentModel.Win32Exception(task.LastTaskResult)) : new Exception($"Scheduled task exited with code {task.LastTaskResult}"); ActionResponse response = new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), e, "TaskSchedulerRunFailed"); response.ExceptionDetail.LogLocation = FileUtility.GetLocalTemplatePath(request.Info.AppName); return(response); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { string taskName = request.DataStore.GetValue("TaskName"); TaskCollection tasks = null; using (TaskService ts = new TaskService()) { tasks = ts.RootFolder.GetTasks(new Regex(taskName)); // We expect only one task to match foreach (Task task in tasks) { switch (task.LastTaskResult) { case 0: return(new ActionResponse(ActionStatus.Success, JsonUtility.GetEmptyJObject())); case 267014: //zipLogs(); return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), new Exception("The scheduled task was terminated by the user."), "TaskSchedulerRunFailed")); case 411: return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), new Exception("PowerShell version too low - please upgrade to latest version https://msdn.microsoft.com/en-us/powershell/wmf/5.0/requirements"), "TaskSchedulerRunFailed")); } if (task.State == TaskState.Running) { return(new ActionResponse(ActionStatus.BatchNoState, JsonUtility.GetEmptyJObject())); } if (NTHelper.IsCredentialGuardEnabled()) { return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "CredentialGuardEnabled")); } //If we've encountered an error, copy the logs, zip them up, and send to blob //zipLogs(); ActionResponse response = new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), new Exception($"Scheduled task exited with code {task.LastTaskResult}"), "TaskSchedulerRunFailed"); response.ExceptionDetail.LogLocation = FileUtility.GetLocalTemplatePath(request.Info.AppName); return(response); } } // We should never return this return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "SccmTaskNotFound")); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { // The status could change if we're patient System.Threading.Thread.Sleep(250); Task task = GetScheduledTask(request.DataStore.GetValue("TaskName")); if (task == null) { return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "SccmTaskNotFound")); } // Let it run if (task.State == TaskState.Queued || task.State == TaskState.Running || task.State == TaskState.Unknown) { return(new ActionResponse(ActionStatus.InProgress)); } switch (task.LastTaskResult) { case 0: return(new ActionResponse(ActionStatus.Success)); case 0x00041303: // SCHED_S_TASK_HAS_NOT_RUN: The task has not yet run. case 0x00041325: // SCHED_S_TASK_QUEUED: The Task Scheduler service has asked the task to run return(new ActionResponse(ActionStatus.InProgress)); default: // there was an error since we haven't exited above uploadLogs(request); if (NTHelper.IsCredentialGuardEnabled()) { return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "CredentialGuardEnabled")); } // azurebcp exits with 0, 1, 2, the powershell script might return 1 - anything else must be a Windows error Exception e = (uint)task.LastTaskResult > 2 ? new Exception($"Scheduled task exited with code {task.LastTaskResult}", new System.ComponentModel.Win32Exception(task.LastTaskResult)) : new Exception($"Scheduled task exited with code {task.LastTaskResult}"); ActionResponse response = new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), e, "TaskSchedulerRunFailed"); response.ExceptionDetail.LogLocation = FileUtility.GetLocalTemplatePath(request.Info.AppName); return(response); } }