在 Windows 任务计划程序中更改已计划任务的运行时间

发布于 2024-12-22 13:52:12 字数 1405 浏览 0 评论 0原文

我在修改机器上已存在的任务时遇到问题。我正在尝试使用 C# 生成的互操作接口(从 system32/taskschd.dll 生成的 Interop.TaskScheduler.dll)来执行此操作。

首先,我无法使用其他库,例如 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);

乍一看它应该可以工作,但是由于某种原因,当我尝试分配新的日期时间以在线运行时:

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

它不起作用。实际上,在该 foreach 中它已更改,但是当我尝试调试并创建另一个打印 StartBoundary 值的 foreach 时,它显示旧值。我做错了什么吗?有机会让它发挥作用吗? :-) 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请持续率性 2024-12-29 13:52:12

如果您尝试像这样使用任务计划程序 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, ...

答案归原发者所有,请参阅问题评论。我写这个答案是为了澄清问题,并提请注意这个问题已经得到解答,因为类似的问题困扰了我几天。

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.

格子衫的從容 2024-12-29 13:52:12
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;
        }

克拉克·博斯

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

梨涡少年 2024-12-29 13:52:12

是的。您必须创建一个具有相同标题和文件夹的新任务定义才能更新现有任务定义。尝试注册它并在任务计划程序中查看该任务。它应该已经用新的时间表更新了任务。但是,历史应该保持不变。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文