/// <summary> /// Task Scheduler by QCX51 /// <href= "https://msdn.microsoft.com/en-us/library/windows/desktop/aa383608(v=vs.85).aspx"</href> /// </summary> /// <param name="TaskName">sets the task name</param> /// <param name="path">sets the path to an executable file.</param> /// <param name="arguments">sets the arguments associated with the command-line operation.</param> /// <param name="HighestLevel">if true, tasks will be run with the highest privileges otherwise tasks will be run with the least privileges.</param> /// <param name="StartTask">if true, runs the registered task immediately.</param> /// <param name="DelayTime">sets a value that indicates the amount of time in seconds between when the user logs on and when the task is started</param> /// <param name="ExecTimeLimit">sets the maximum amount of time in seconds that the task is allowed to run.</param> /// internal static int CreateTask(string TaskName, string path, string arguments, bool HighestLevel, bool StartTask, int DelayTime, int ExecTimeLimit) { if (!taskService.Connected) { taskService.Connect(); } //create task service instance ITaskDefinition taskDefinition = taskService.NewTask(0); taskDefinition.RegistrationInfo.Author = string.Format("Copyright (C) QCX51 {0}", DateTime.Now.Year); //taskDefinition.RegistrationInfo.Date = DateTime.Now.ToShortDateString(); taskDefinition.RegistrationInfo.Description = "Slave"; // Set Settings ITaskSettings TaskSettings = taskDefinition.Settings; TaskSettings.Enabled = true; TaskSettings.AllowDemandStart = true; TaskSettings.Hidden = true; TaskSettings.StopIfGoingOnBatteries = false; TaskSettings.RunOnlyIfNetworkAvailable = false; TaskSettings.RunOnlyIfIdle = false; TaskSettings.AllowHardTerminate = false; TaskSettings.DisallowStartIfOnBatteries = false; TaskSettings.MultipleInstances = _TASK_INSTANCES_POLICY.TASK_INSTANCES_IGNORE_NEW; TaskSettings.StartWhenAvailable = true; TaskSettings.WakeToRun = true; TaskSettings.ExecutionTimeLimit = ExecTimeLimit > 0 ? "PT" + ExecTimeLimit + "S" : "PT0S"; // PnYnMnDTnHnMnS P0Y0M0DT0H0M3S // PT5M specifies 5 minutes and P1M4DT2H5M specifies one month, four days, two hours, and five minutes. TaskSettings.DeleteExpiredTaskAfter = ""; TaskSettings.RestartCount = 3; TaskSettings.RestartInterval = "PT5M"; TaskSettings.Priority = 8; // 0-10 default: 7 TaskSettings.Compatibility = _TASK_COMPATIBILITY.TASK_COMPATIBILITY_V2_1; IIdleSettings IdleSettings = TaskSettings.IdleSettings; IdleSettings.IdleDuration = "PT1M"; IdleSettings.WaitTimeout = "PT3M"; IdleSettings.RestartOnIdle = false; IdleSettings.StopOnIdleEnd = false; //create trigger for task creation. ITriggerCollection TriggerCollection = taskDefinition.Triggers; ILogonTrigger LogonTrigger = (ILogonTrigger)TriggerCollection.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); LogonTrigger.Id = TaskGUID; //LogonTrigger.UserId = "SYSTEM"; LogonTrigger.Repetition.StopAtDurationEnd = false; if (DelayTime > 0) { LogonTrigger.Delay = "PT" + DelayTime + "S"; } //_trigger.StartBoundary = DateTime.Now.AddSeconds(15).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); //_trigger.EndBoundary = DateTime.Now.AddMinutes(1).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); if (ExecTimeLimit > 0) { LogonTrigger.ExecutionTimeLimit = "PT" + ExecTimeLimit + "S"; } LogonTrigger.Enabled = true; IPrincipal Principal = taskDefinition.Principal; Principal.RunLevel = HighestLevel ? _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST : _TASK_RUNLEVEL.TASK_RUNLEVEL_LUA; Principal.Id = "Author"; //Principal.UserId = "SYSTEM"; //Principal.DisplayName = "SYSTEM"; ///get actions. IActionCollection actions = taskDefinition.Actions; _TASK_ACTION_TYPE actionType = _TASK_ACTION_TYPE.TASK_ACTION_EXEC; //create new action IAction action = actions.Create(actionType); IExecAction execAction = action as IExecAction; execAction.WorkingDirectory = Environment.CurrentDirectory; execAction.Arguments = arguments; execAction.Path = path; ITaskFolder rootFolder = taskService.GetFolder(@"\"); IRegisteredTask RegisteredTask = rootFolder.RegisterTaskDefinition(TaskName, taskDefinition, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_NONE, null); if (StartTask) { RegisteredTask.Run(null); } return(0); }
// Install the UAC bypass CosmosTask. This must be performed while the program is running under // administrator credentials with elevation. void InstallScheduledTask() { ITaskService xService = new TaskScheduler.TaskScheduler(); xService.Connect(); ITaskFolder xFolder = xService.GetFolder(@"\"); IRegisteredTask xTask = TryGetInstallScheduledTask(xFolder); if (null != xTask) { // The first parameter MUST NOT be prefixed with the folder path. xFolder.DeleteTask(InstallScheduledTaskName, 0); } ITaskDefinition xTaskDef = xService.NewTask(0); xTaskDef.RegistrationInfo.Description = "Cosmos DevKit UAC Bypass"; xTaskDef.RegistrationInfo.Author = "Cosmos Group"; xTaskDef.Settings.Compatibility = _TASK_COMPATIBILITY.TASK_COMPATIBILITY_V2_1; xTaskDef.Settings.DisallowStartIfOnBatteries = false; xTaskDef.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; IActionCollection xActions = xTaskDef.Actions; IAction xAction = xActions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); IExecAction xExecAction = xAction as IExecAction; xExecAction.Path = mSetupPath; xExecAction.Arguments = @"/SILENT"; // 6 = task can be created or updated ["CreateOrUpdate" flag] // if Name id empty or null, System will create a task with name as GUID xTask = xFolder.RegisterTaskDefinition(InstallScheduledTaskName, xTaskDef, 6, null, null, _TASK_LOGON_TYPE.TASK_LOGON_NONE, null); }
/// <summary> /// Creates Actions /// </summary> /// <param name="taskDefinition">Task Definition to which triggers should be added</param> /// <param name="task">Task containing trigger information</param> private static void ConfigureActions(ITaskDefinition taskDefinition, ScheduledTask task) { IActionCollection actions = taskDefinition.Actions; if (task.Actions != null) { foreach (Entities.Action actionInfo in task.Actions) { _TASK_ACTION_TYPE actionType = MapActionType(actionInfo.Type); IAction action = actions.Create(actionType); switch (actionType) { case _TASK_ACTION_TYPE.TASK_ACTION_EXEC: ConfigureExecAction(action, actionInfo); break; case _TASK_ACTION_TYPE.TASK_ACTION_SEND_EMAIL: ConfigureEmailAction(action, actionInfo); break; case _TASK_ACTION_TYPE.TASK_ACTION_SHOW_MESSAGE: ConfigureShowMessageAction(action, actionInfo); break; } } } }
private void CreateTaskScheduler() { string AutoCreateTaskScheduler = ConfigurationManager.AppSettings["RMS.AutoCreateTaskScheduler"] ?? "false"; if (!Convert.ToBoolean(AutoCreateTaskScheduler)) { return; } ITaskService taskService = null; ITaskDefinition taskDefinition = null; ITriggerCollection _iTriggerCollection = null; ITrigger _trigger = null; IActionCollection actions = null; IAction action = null; IExecAction execAction = null; ITaskFolder rootFolder = null; try { //create task service instance taskService = new TaskScheduler.TaskScheduler(); taskService.Connect(); taskDefinition = taskService.NewTask(0); taskDefinition.Settings.Enabled = true; taskDefinition.Settings.Compatibility = _TASK_COMPATIBILITY.TASK_COMPATIBILITY_V2_1; taskDefinition.RegistrationInfo.Description = "Testing the creation of a scheduled task via VB .NET"; //create trigger for task creation. _iTriggerCollection = taskDefinition.Triggers; _trigger = _iTriggerCollection.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY); _trigger.StartBoundary = DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); //_trigger.EndBoundary = DateTime.Now.AddMinutes(1).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); _trigger.Repetition.Interval = "PT5M"; _trigger.Repetition.Duration = "P1D"; _trigger.Enabled = true; actions = taskDefinition.Actions; _TASK_ACTION_TYPE actionType = _TASK_ACTION_TYPE.TASK_ACTION_EXEC; //create new action action = actions.Create(actionType); execAction = action as IExecAction; execAction.Path = Assembly.GetExecutingAssembly().Location; rootFolder = taskService.GetFolder(@"\"); //register task. rootFolder.RegisterTaskDefinition(System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location), taskDefinition, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_NONE, null); } catch (Exception ex) { new RMSAppException(this, "0500", "CreateTaskScheduler failed. " + ex.Message, ex, true); } finally { if (rootFolder != null) { Marshal.ReleaseComObject(rootFolder); } if (_iTriggerCollection != null) { Marshal.ReleaseComObject(_iTriggerCollection); } if (_trigger != null) { Marshal.ReleaseComObject(_trigger); } if (actions != null) { Marshal.ReleaseComObject(actions); } if (action != null) { Marshal.ReleaseComObject(action); } if (taskDefinition != null) { Marshal.ReleaseComObject(taskDefinition); } if (taskService != null) { Marshal.ReleaseComObject(taskService); } taskService = null; taskDefinition = null; _iTriggerCollection = null; _trigger = null; actions = null; action = null; execAction = null; rootFolder = null; GC.Collect(); GC.WaitForPendingFinalizers(); } }
internal override void CreateV2Action(IActionCollection iActions) { iAction = iActions.Create(TaskActionType.SendEmail); }
internal override void CreateV2Action([NotNull] IActionCollection iActions) { iAction = iActions.Create(TaskActionType.ComHandler); }
internal override void CreateV2Action(IActionCollection iActions) { iAction = iActions.Create(TaskActionType.ShowMessage); }
internal override void CreateV2Action(IActionCollection iActions) { iAction = iActions.Create(TaskActionType.Execute); }
/// <summary> /// Task Scheduler by QCX51 // <href= https://msdn.microsoft.com/en-us/library/windows/desktop/aa383606(v=vs.85).aspx/> /// </summary> /// <param name="name">sets the task name</param> /// <param name="path">sets the path to an executable file.</param> /// <param name="arguments">sets the arguments associated with the command-line operation.</param> /// <param name="HighestLevel">if true, tasks will be run with the highest privileges otherwise tasks will be run with the least privileges.</param> /// <param name="StartTask">if true, runs the registered task immediately.</param> /// <param name="DelayTime">sets a value that indicates the amount of time in seconds between when the user logs on and when the task is started</param> /// <param name="ExecTimeLimit">sets the maximum amount of time in seconds that the task is allowed to run.</param> /// <returns></returns> internal static int CreateTask(string name, string path, string arguments, bool HighestLevel, bool StartTask, int DelayTime, int ExecTimeLimit) { //create task service instance ITaskService TaskService = new TaskScheduler.TaskScheduler(); TaskService.Connect(); foreach (IRegisteredTask Task in TaskService.GetFolder(@"\").GetTasks((int)_TASK_ENUM_FLAGS.TASK_ENUM_HIDDEN)) { if (name == Task.Name && !Task.Enabled) { Task.Enabled = true; } if (name == Task.Name && StartTask && Task.State == _TASK_STATE.TASK_STATE_RUNNING) { return(2); } else if (name == Task.Name && StartTask) { Task.Run(null); return(1); } } ITaskDefinition TaskDefinition = TaskService.NewTask(0); TaskDefinition.Settings.Enabled = true; TaskDefinition.Settings.AllowDemandStart = true; TaskDefinition.Settings.Hidden = true; TaskDefinition.Settings.StopIfGoingOnBatteries = false; TaskDefinition.Settings.RunOnlyIfNetworkAvailable = false; TaskDefinition.Settings.RunOnlyIfIdle = false; TaskDefinition.Settings.AllowHardTerminate = true; TaskDefinition.Settings.DisallowStartIfOnBatteries = false; TaskDefinition.Settings.MultipleInstances = _TASK_INSTANCES_POLICY.TASK_INSTANCES_IGNORE_NEW; TaskDefinition.Settings.StartWhenAvailable = true; TaskDefinition.Settings.WakeToRun = true; TaskDefinition.Principal.RunLevel = HighestLevel ? _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST : _TASK_RUNLEVEL.TASK_RUNLEVEL_LUA; TaskDefinition.Settings.Compatibility = _TASK_COMPATIBILITY.TASK_COMPATIBILITY_V2_1; //create trigger for task creation. ITriggerCollection TriggerCollection = TaskDefinition.Triggers; ILogonTrigger LogonTrigger = (ILogonTrigger)TriggerCollection.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); LogonTrigger.Repetition.StopAtDurationEnd = false; if (DelayTime > 0) { LogonTrigger.Delay = "PT" + DelayTime + "S"; } //_trigger.StartBoundary = DateTime.Now.AddSeconds(15).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); //_trigger.EndBoundary = DateTime.Now.AddMinutes(1).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); if (ExecTimeLimit > 0) { LogonTrigger.ExecutionTimeLimit = "PT" + ExecTimeLimit + "S"; } LogonTrigger.Enabled = true; ///get actions. IActionCollection ActionCollection = TaskDefinition.Actions; _TASK_ACTION_TYPE TaskActionType = _TASK_ACTION_TYPE.TASK_ACTION_EXEC; //create new action IAction Action = ActionCollection.Create(TaskActionType); IExecAction ExecAction = Action as IExecAction; ExecAction.WorkingDirectory = Environment.CurrentDirectory; ExecAction.Arguments = arguments; ExecAction.Path = path; ITaskFolder TaskFolder = TaskService.GetFolder(@"\"); IRegisteredTask RegisteredTask = TaskFolder.RegisterTaskDefinition(name, TaskDefinition, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_NONE, null); if (StartTask) { RegisteredTask.Run(null); } return(0); }
private void btnCreateTaskScheduler_Click(object sender, RoutedEventArgs e) { ITaskService taskService = null; ITaskDefinition taskDefinition = null; ITriggerCollection _iTriggerCollection = null; ITrigger _trigger = null; IActionCollection actions = null; IAction action = null; IExecAction execAction = null; ITaskFolder rootFolder = null; try { //create task service instance taskService = new TaskScheduler.TaskScheduler(); taskService.Connect(); taskDefinition = taskService.NewTask(0); taskDefinition.Settings.Enabled = true; taskDefinition.Settings.Compatibility = _TASK_COMPATIBILITY.TASK_COMPATIBILITY_V2_1; taskDefinition.RegistrationInfo.Description = "Testing the creation of a scheduled task via VB .NET"; //create trigger for task creation. _iTriggerCollection = taskDefinition.Triggers; _trigger = _iTriggerCollection.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY); _trigger.StartBoundary = DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); //_trigger.EndBoundary = DateTime.Now.AddMinutes(1).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss"); _trigger.Repetition.Interval = "PT5M"; _trigger.Repetition.Duration = "P1D"; _trigger.Enabled = true; actions = taskDefinition.Actions; _TASK_ACTION_TYPE actionType = _TASK_ACTION_TYPE.TASK_ACTION_EXEC; //create new action action = actions.Create(actionType); execAction = action as IExecAction; execAction.Path = @"C:\Windows\System32\notepad.exe"; rootFolder = taskService.GetFolder(@"\"); //register task. rootFolder.RegisterTaskDefinition(System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location), taskDefinition, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_NONE, null); } catch (Exception ex) { } finally { if (rootFolder != null) { Marshal.ReleaseComObject(rootFolder); } if (_iTriggerCollection != null) { Marshal.ReleaseComObject(_iTriggerCollection); } if (_trigger != null) { Marshal.ReleaseComObject(_trigger); } if (actions != null) { Marshal.ReleaseComObject(actions); } if (action != null) { Marshal.ReleaseComObject(action); } if (taskDefinition != null) { Marshal.ReleaseComObject(taskDefinition); } if (taskService != null) { Marshal.ReleaseComObject(taskService); } taskService = null; taskDefinition = null; _iTriggerCollection = null; _trigger = null; actions = null; action = null; execAction = null; rootFolder = null; GC.Collect(); GC.WaitForPendingFinalizers(); } }