Changing run time of already scheduled tasks in wi

2020-03-26 04:49发布

I have problem with modifying tasks which already exists on machine. I'm trying to do this with generated interop interfaces from C# (Interop.TaskScheduler.dll generated from system32/taskschd.dll).

To start with, I can't use other libraries like http://taskscheduler.codeplex.com/. Already tested and it works with library mentioned before. Now when I try do same with generated interfaces nothing changes. Basically what I'm doing:

string STR_DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
string taskName = "taskName", 
       user = "user", 
       pass = "pass";

DateTime nextRun = new DateTime.Now.AddDays(7);

TaskSchedulerClass ts = new TaskSchedulerClass();
ts.Connect(null, null, null, null);
IRegisteredTask task = ts.GetFolder("\\").GetTask(String.Format("\\{0}",taskName));

foreach (ITrigger t in task.Definition.Triggers)
    t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));

ts.GetFolder("\\").RegisterTaskDefinition(task.Path, 
                            task.Definition, 
                            (int)_TASK_CREATION.TASK_UPDATE, 
                            user, 
                            pass, 
                            _TASK_LOGON_TYPE.TASK_LOGON_PASSWORD, 
                            null);

For first look it should be working, but for some reason when I try to assign new datetime for run in line:

t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));

It doesn't work. Actually in that foreach it's changed but when I tried to debug and created another foreach which prints value of StartBoundary it shows old value. Am I doing something incorrect? Is there any chance to get it working? :-) Thank you.

3条回答
闹够了就滚
2楼-- · 2020-03-26 04:53
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

查看更多
男人必须洒脱
3楼-- · 2020-03-26 05:05

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.

查看更多
SAY GOODBYE
4楼-- · 2020-03-26 05:10

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.

查看更多
登录 后发表回答