private void wizardControl1_Finished(object sender, System.EventArgs e) { if (TaskService == null) { TaskService = TaskService.Instance; } td.Data = TaskName; td.RegistrationInfo.Description = descText.Text; td.Triggers.Clear(); td.Triggers.Add(trigger); td.Actions.Clear(); td.Actions.Add(action); if (openDlgAfterCheck.Checked) { var dlg = new TaskEditDialog(TaskService, td, true, false, TaskName) { StartPosition = FormStartPosition.CenterParent }; if (dlg.ShowDialog(ParentForm) == DialogResult.OK) { td = dlg.TaskDefinition; } } if (RegisterTaskOnFinish) { TaskFolder fld = TaskService.RootFolder; if (!string.IsNullOrEmpty(TaskFolder) && TaskService.HighestSupportedVersion.CompareTo(TaskServiceVersion.V1_1) != 0) { fld = TaskService.GetFolder(TaskFolder); } task = fld.RegisterTaskDefinition(TaskName, td, TaskCreation.CreateOrUpdate, td.Principal.ToString(), Password, td.Principal.LogonType); } }
public void Init() { _service = new TaskService(); _folder = _service.RootFolder.SubFolders.Any(a => a.Name == "WarewolfTestFolder") ? _service.GetFolder("WarewolfTestFolder") : _service.RootFolder.CreateFolder("WarewolfTestFolder"); var task = _service.NewTask(); task.Actions.Add(new ExecAction("Notepad.exe")); _folder.RegisterTaskDefinition("TestTask", task); }
public static bool PersistTask(bool startAtLogon, bool startForAllUsers) { WinTasks.TaskService ts = new WinTasks.TaskService(); try { WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent(); bool isElevated = (new WindowsPrincipal(currentIdentity).IsInRole(WindowsBuiltInRole.Administrator)); WinTasks.Task task = ts.FindTask(Application.ProductName, false); if (startAtLogon) { if (startForAllUsers && !isElevated) { return PersistTaskElevated(startAtLogon, startForAllUsers); } else { WinTasks.LogonTrigger trigger = (WinTasks.LogonTrigger)WinTasks.LogonTrigger.CreateTrigger(WinTasks.TaskTriggerType.Logon); trigger.Enabled = true; trigger.StartBoundary = DateTime.Today; if (startForAllUsers) trigger.UserId = null; else trigger.UserId = currentIdentity.Name; WinTasks.ExecAction action = (WinTasks.ExecAction)WinTasks.ExecAction.CreateAction(WinTasks.TaskActionType.Execute); action.Path = Application.ExecutablePath; action.WorkingDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath); if (task == null) { task = ts.AddTask(Application.ProductName, trigger, action, currentIdentity.Name); } else { task.Definition.Triggers.Clear(); task.Definition.Triggers.Add(trigger); task.Definition.Actions.Clear(); task.Definition.Actions.Add(action); task.RegisterChanges(); } } } else if (task != null) { ts.GetFolder("\\").DeleteTask(task.Name); } } catch { return false; } finally { ts.Dispose(); } return true; }
private static TaskFolder loadTaskFolder() { TaskService ts = new TaskService(); TaskFolder folder; try { folder = ts.GetFolder(@"\"+Constants.TASK_SCHEDULER_FOLDER); } catch (FileNotFoundException) { folder = ts.RootFolder.CreateFolder(@"\"+Constants.TASK_SCHEDULER_FOLDER); } return folder; }
/// <summary> /// Handles the Click event of the okBtn control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> /// <remarks>Changed in release 1.8.4 so that when a user cancels the password dialog, it no longer throws an exception but rather displays an error.</remarks> private void okBtn_Click(object sender, System.EventArgs e) { if (TaskDefinition.Actions.Count == 0) { MessageBox.Show(EditorProperties.Resources.TaskMustHaveActionsError, null, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (TaskDefinition.Settings.DeleteExpiredTaskAfter != TimeSpan.Zero && !ValidateOneTriggerExpires()) { MessageBox.Show(EditorProperties.Resources.Error_TaskDeleteMustHaveExpiringTrigger, null, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (TaskDefinition.LowestSupportedVersion > TaskDefinition.Settings.Compatibility) { MessageBox.Show(EditorProperties.Resources.Error_TaskPropertiesIncompatibleSimple, null, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (RegisterTaskOnAccept) { if (Task != null && !TaskDefinition.Principal.RequiresPassword()) { Task.RegisterChanges(); } else { var user = TaskDefinition.Principal.ToString(); user = string.IsNullOrEmpty(user) ? WindowsIdentity.GetCurrent().Name : user; string pwd = null; TaskFolder fld = TaskService.GetFolder(TaskFolder); if (TaskDefinition.Principal.RequiresPassword()) { pwd = InvokeCredentialDialog(user, this); if (pwd == null) { //throw new System.Security.Authentication.AuthenticationException(EditorProperties.Resources.UserAuthenticationError); MessageBox.Show(EditorProperties.Resources.Error_PasswordMustBeProvided, null, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } Task = fld.RegisterTaskDefinition(taskPropertiesControl1.TaskName, TaskDefinition, TaskCreation.CreateOrUpdate, user, pwd, TaskDefinition.Principal.LogonType); } } DialogResult = DialogResult.OK; Close(); }
public static ActionResult CleanTasks(Session session) { try { var taskService = new TaskService(); var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList(); foreach (var task in existingTasks) taskService.RootFolder.DeleteTask(@"FOG\" + task.Name); taskService.RootFolder.DeleteFolder("FOG", false); } catch (Exception ) { } return ActionResult.Success; }
private void wizardControl1_Finished(object sender, System.EventArgs e) { bool myTS = false; if (TaskService == null) { TaskService = new TaskService(); myTS = true; } td.Data = TaskName; td.RegistrationInfo.Description = descText.Text; td.Triggers.Clear(); td.Triggers.Add(trigger); td.Actions.Clear(); td.Actions.Add(action); if (openDlgAfterCheck.Checked) { TaskEditDialog dlg = new TaskEditDialog(); dlg.Editable = true; dlg.Initialize(TaskService, td); dlg.RegisterTaskOnAccept = false; dlg.TaskName = TaskName; if (dlg.ShowDialog(ParentForm) == DialogResult.OK) { td = dlg.TaskDefinition; } } if (RegisterTaskOnFinish) { TaskFolder fld = TaskService.RootFolder; if (!string.IsNullOrEmpty(TaskFolder) && TaskService.HighestSupportedVersion.CompareTo(new Version(1, 1)) != 0) { fld = TaskService.GetFolder(TaskFolder); } task = fld.RegisterTaskDefinition(TaskName, td, TaskCreation.CreateOrUpdate, td.Principal.ToString(), Password, td.Principal.LogonType); } if (myTS) { TaskService = null; } }
private static bool CheckTaskName(string taskName) { bool result = false; using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) { using (Microsoft.Win32.TaskScheduler.TaskFolder taskFolder = ts.GetFolder(@"\")) { foreach (Task t in taskFolder.Tasks) { if (t.Name.Contains(taskName)) { result = true; } } } } return(result); }
private void RefreshTasks() { DGV_taskBackumsoutlook.Rows.Clear(); using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) { using (Microsoft.Win32.TaskScheduler.TaskFolder taskFolder = ts.GetFolder(@"\")) { foreach (Task t in taskFolder.Tasks) { if (t.Name.Contains("backupmsoutlook_")) { TaskDefinition td = t.Definition; string lastRun = t.LastRunTime.ToString(); if (lastRun.Contains("1899")) { lastRun = string.Empty; } string actions = td.Actions[0].ToString(); if (actions.Contains("\t")) { actions = actions.Replace("\t", " -> "); } string[] row = new string[] { t.Name, t.State.ToString(), actions, t.NextRunTime.ToString(), lastRun, "0x" + t.LastTaskResult, td.RegistrationInfo.Author, td.RegistrationInfo.Date.ToString() }; DGV_taskBackumsoutlook.Rows.Add(row); DGV_taskBackumsoutlook.Rows[DGV_taskBackumsoutlook.Rows.Count - 1].Selected = true; } } } } if (DGV_taskBackumsoutlook.Rows.Count > 0) { DGV_taskBackumsoutlook.Rows[0].Cells[0].Selected = false; DGV_taskBackumsoutlook.Rows[DGV_taskBackumsoutlook.Rows.Count - 1].Selected = true; } }
public static ActionResult CleanTasks(Session session) { try { var config = GetSettings(); if (GetValue("LIGHT", config).Equals("1")) return ActionResult.Success; var taskService = new TaskService(); var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList(); foreach (var task in existingTasks) taskService.RootFolder.DeleteTask(@"FOG\" + task.Name); taskService.RootFolder.DeleteFolder("FOG", false); taskService.Dispose(); } catch (Exception) { // ignored } return ActionResult.Success; }
private List<string> FilterTasks(List<string> newTasks) { var taskService = new TaskService(); try { taskService.RootFolder.CreateFolder("FOG"); } catch (Exception) { } var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList(); foreach (var task in existingTasks) if (!newTasks.Contains(task.Name)) { Log.Entry(Name, "Delete task " + task.Name); taskService.RootFolder.DeleteTask(@"FOG\" + task.Name); //If the existing task is not in the new list delete it } else { Log.Entry(Name, task.Name + " already scheduled"); newTasks.Remove(task.Name); //Remove the existing task from the queue } return newTasks; }
private TaskFolder GetTaskFolder(TaskService ts, string folderName) { TaskFolder sysconTaskFolder = null; try { sysconTaskFolder = ts.GetFolder("Syscon"); } catch (Exception ex) { Env.Log("Could not find the Syscon task scheduler folder. Trying to create one."); } return sysconTaskFolder; }
public void WhenTheIsExecutedTimes(string scheduleName, int times) { try { int i = 0; var x = new TaskService(); x.GetFolder("Warewolf"); var task = x.FindTask(scheduleName); do { task.Run(); const int TimeOut = 10; int time = 0; while(task.State == TaskState.Running && time < TimeOut) { time++; Thread.Sleep(1000); } i++; } while(i < times); } catch(Exception e) { ScenarioContext.Current["Error"] = e; } }
internal static void WizardTest(TaskService ts, System.IO.TextWriter output, params string[] arg) { try { string FolderName = "My Folder"; bool v2 = ts.HighestSupportedVersion > new Version(1, 1); var taskFolder = ts.RootFolder; if (v2) { try { taskFolder = ts.GetFolder(FolderName); } catch (System.IO.FileNotFoundException) { taskFolder = ts.RootFolder.CreateFolder(FolderName); } } using (var taskSchedulerWizard = new TaskSchedulerWizard()) { var newTaskDefinition = ts.NewTask(); newTaskDefinition.Actions.Add(new ExecAction("notepad.exe")); taskSchedulerWizard.Initialize(ts, newTaskDefinition); taskSchedulerWizard.TaskFolder = FolderName; taskSchedulerWizard.RegisterTaskOnFinish = true; taskSchedulerWizard.AvailableTriggers = TaskSchedulerWizard.AvailableWizardTriggers.Event; taskSchedulerWizard.AvailablePages = TaskSchedulerWizard.AvailableWizardPages.IntroPage | //TaskSchedulerWizard.AvailableWizardPages.TriggerSelectPage | TaskSchedulerWizard.AvailableWizardPages.TriggerEditPage | //TaskSchedulerWizard.AvailableWizardPages.TriggerPropertiesPage | TaskSchedulerWizard.AvailableWizardPages.ActionEditPage | //TaskSchedulerWizard.AvailableWizardPages.SecurityPage | TaskSchedulerWizard.AvailableWizardPages.SummaryPage; if (taskSchedulerWizard.ShowDialog() == DialogResult.OK) taskFolder.DeleteTask(taskSchedulerWizard.Task.Name); // _tlv.Tasks = taskFolder.Tasks; } // Create a new task definition and assign properties /*TaskSchedulerWizard wiz = new TaskSchedulerWizard(ts, null, true) { TaskFolder = @"\Microsoft" }; if (wiz.ShowDialog() == DialogResult.OK) { Task t = wiz.Task; if (t.Definition.Triggers.Count > 1) new TaskEditDialog(t).ShowDialog(); else { wiz.AvailablePages = TaskSchedulerWizard.AvailableWizardPages.TriggerPropertiesPage | TaskSchedulerWizard.AvailableWizardPages.TriggerSelectPage | TaskSchedulerWizard.AvailableWizardPages.SummaryPage; wiz.AvailableTriggers = TaskSchedulerWizard.AvailableWizardTriggers.Daily | TaskSchedulerWizard.AvailableWizardTriggers.Time | TaskSchedulerWizard.AvailableWizardTriggers.Weekly | TaskSchedulerWizard.AvailableWizardTriggers.Monthly | TaskSchedulerWizard.AvailableWizardTriggers.MonthlyDOW; wiz.AllowEditorOnFinish = true; wiz.EditorOnFinishText = "Show dialog"; wiz.TriggerPagePrompt = "When???"; wiz.RegisterTaskOnFinish = true; wiz.SummaryRegistrationNotice = "Done when you click Finish"; wiz.SummaryFormatString = "Name: {0}\r\nDescription: {1}\r\nTrigger: {2}"; wiz.Title = "My Wizard"; wiz.Initialize(t); wiz.ShowDialog(); } } if (wiz.Task != null) ts.RootFolder.DeleteTask(wiz.Task.Path);*/ } catch (Exception ex) { output.WriteLine(ex.ToString()); } }
static void disableRunningZombieTasks() { using (TaskService ts = new TaskService()) { if (!ts.Connected) { RunningTaskCollection runningTasks = ts.GetRunningTasks(); TaskFolder KNXFolder = ts.GetFolder("KNX"); for (var i = 0; i < runningTasks.Count; i++) { if (runningTasks[i].Folder.Path == "\\KNX") { if (runningTasks[i].NextRunTime < DateTime.Now) { runningTasks[i].Enabled = false; runningTasks[i].Stop(); } } } } } }
public void ClearAll() { var taskService = new TaskService(); try { taskService.RootFolder.CreateFolder("FOG"); } catch (Exception) { // ignored } var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList(); foreach (var task in existingTasks) { Log.Debug(LogName, "Delete task " + task.Name); taskService.RootFolder.DeleteTask(@"FOG\" + task.Name); } taskService.Dispose(); }