Changing run time of already scheduled tasks in windows task scheduler

折月煮酒 提交于 2019-12-01 09:15:54

If you are attempting to update a task using the Task Scheduler C# API like this, you need to declare a new ITaskDefinition. If you attempt to change the existing definition and then re-Register, it does not work.

IRegisteredTask oldTask = ...
ITaskDefinition task = oldTask.Definition;

//modifications to oldTask.Definition / task

//does **not** work
folder.RegisterTaskDefinition(oldTask.Name, oldTask.Definition, ...
//does work
folder.RegisterTaskDefinition(oldTask.Name, task, ...

Answer credit goes to original poster, see comment on question. I wrote up this answer to clarify the issue, and draw attention to the fact that the question had been answered, as a similar issue troubled me for a few days.

public string ModifyScheduledTaskSchedule(string TaskName, string Date, string Hour, string Minute)
        {

            string ReturnedTask = TaskName;


                TaskScheduler.TaskScheduler ts = new TaskScheduler.TaskScheduler();

                ts.Connect(PackagingServer, PkgServerUserName, "DOMAIN", PkgServerPassword);

                IRegisteredTask task = ts.GetFolder("\\").GetTask(TaskName);

                ITaskDefinition td = task.Definition;

                td.Triggers[1].StartBoundary = Date + "T" + Hour + ":" + Minute + ":" + "00";

                ts.GetFolder("\\").RegisterTaskDefinition(TaskName, td, (int)_TASK_CREATION.TASK_UPDATE, "DOMAIN\\" + PkgServerUserName, PkgServerPassword, _TASK_LOGON_TYPE.TASK_LOGON_NONE, "");


            return ReturnedTask;
        }

Clark Bohs

Yes. You have to create a new task definition with the same title and folder to update the existing one. Try to register it and see the task in Task Scheduler. It should have updated the task with the new schedule. But, the history should remain the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!