通过 C# 在任务计划程序中设置用户帐户

本文关键字:设置 用户 程序 任务 计划 通过 | 更新日期: 2023-09-27 17:55:34

我正在尝试使用我的 C# 程序在任务计划程序中设置以下选项。

1. In settings tab of Task scheduler, check the box "If the task fails, restart every 1 minute"
2. In General tab of Task scheduler, under security options, set it to "BigDataService" for "When running the task, use the following user account" and provide its password.

我当前的代码如下所示:

 using (TaskService ts = new TaskService())
 {
    TaskDefinition td = ts.NewTask();
    TimeTrigger trigger = new TimeTrigger();
    var startTime = TimeSpan.Parse(section1["ScheduledTime"]);
    trigger.StartBoundary = DateTime.Today + startTime;
    trigger.Repetition.Interval = TimeSpan.FromDays(1);
    trigger.StartBoundary = DateTime.Now;
    td.Principal.RunLevel = TaskRunLevel.Highest;
    trigger.Id = "Data";
    trigger.Repetition.Interval = TimeSpan.FromDays(1);
    td.Triggers.Add(trigger);
    td.Actions.Add(new ExecAction(@"DataUpload.exe", argument, null));
    var foldername = ts.GetFolder(@"'Bigdata");
    Console.WriteLine(foldername.Path);
    foldername.RegisterTaskDefinition(section1["JobName"], td);
}

新鳕鱼(适用于RegisterTaskDefinition,但是当我添加RerepeatInterval行时,任务不会在BigData文件夹中创建。

using (TaskService ts = new TaskService())
 {
    TaskDefinition td = ts.NewTask();
    TimeTrigger trigger = new TimeTrigger();
    var startTime = TimeSpan.Parse(section1["ScheduledTime"]);
    trigger.StartBoundary = DateTime.Today + startTime;
    trigger.Repetition.Interval = TimeSpan.FromDays(1);
    trigger.StartBoundary = DateTime.Now;
    td.Principal.RunLevel = TaskRunLevel.Highest;
    trigger.Id = "Data";
    trigger.Repetition.Interval = TimeSpan.FromDays(1);
    td.Triggers.Add(trigger);
    td.Actions.Add(new ExecAction(@"DataUpload.exe", argument, null));
    var foldername = ts.GetFolder(@"'Bigdata");
    Console.WriteLine(foldername.Path);   
    foldername.RegisterTaskDefinition(section1["JobName"], td, TaskCreation.CreateOrUpdate,"Bigdataservice", "password");
}

通过 C# 在任务计划程序中设置用户帐户

要将重新启动间隔设置为 1 分钟,请执行以下操作:

td.Settings.RestartInterval = TimeSpan.FromMinutes(1);

要设置登录名:

td.Principal.LogonType = TaskLogonType.Group;
// Pass the credentials when you register the task
foldername.RegisterTaskDefinition(section1["JobName"], td,
    TaskCreation.CreateOrUpdate, "DOMAIN''USERNAME", "Pas$w0rD");