在 Windows 任务计划程序中更改已计划任务的运行时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8592146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing run time of already scheduled tasks in windows task scheduler
提问by mariozski
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).
我在修改机器上已经存在的任务时遇到问题。我正在尝试使用从 C# 生成的互操作接口(从 system32/taskschd.dll 生成的 Interop.TaskScheduler.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:
首先,我不能使用其他库,例如http://taskscheduler.codeplex.com/。已经过测试,它适用于前面提到的库。现在,当我尝试对生成的接口做同样的事情时,没有任何变化。基本上我在做什么:
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.
它不起作用。实际上,在那个 foreach 中它已更改,但是当我尝试调试并创建另一个打印 StartBoundary 值的 foreach 时,它显示旧值。我做错了什么吗?有没有机会让它工作?:-) 谢谢。
回答by Sam DeHaan
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.
如果您尝试像这样使用 Task Scheduler C# API 更新任务,则需要声明一个新的 ITaskDefinition。如果您尝试更改现有定义,然后重新注册,则不起作用。
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.
答案归功于原始海报,请参阅问题评论。我写了这个答案来澄清这个问题,并提请注意这个问题已经得到回答的事实,因为类似的问题困扰了我几天。
回答by Clark Bohs
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
克拉克·博斯
回答by Sai
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.
是的。您必须创建一个具有相同标题和文件夹的新任务定义才能更新现有任务定义。尝试注册它并在任务计划程序中查看任务。它应该已经用新的计划更新了任务。但是,历史应该保持不变。