1

我以编程方式创建 TaskScheduler 的任务并运行它。我正在使用以下代码:

var taskDefinition = taskService.NewTask();
taskDefinition.RegistrationInfo.Author = WindowsIdentity.GetCurrent().Name;
taskDefinition.RegistrationInfo.Description = "Runs Programm";

// TaskLogonType.S4U = run wether user is logged on or not 
taskDefinition.Principal.LogonType = TaskLogonType.S4U;

var action = new ExecAction(path, arguments);
taskDefinition.Actions.Add(action);
taskService.RootFolder.RegisterTaskDefinition("TaskName", taskDefinition);

//get task:
var task = taskService.RootFolder.GetTasks().Where(a => a.Name == "TaskName").FirstOrDefault();

try
{
    task.Run();
}
catch (Exception ex)
{
    log.Error("Error starting task in TaskSheduler with message: " + ex.Message);
}

任务已创建,我可以在 TaskScheduler 窗口中看到它,但我希望取消选中一个复选框。它被称为“不存储密码。任务将只能访问本地资源”

我发现了如何检查上面的单选按钮,上面写着“无论用户是否登录都运行”这是通过以下方式设置的:

taskDefinition.Principal.LogonType = TaskLogonType.S4U;

但是我如何也将下面的复选框设置为 false?

4

2 回答 2

3

我终于通过以下方式解决了这个问题:

taskService.RootFolder.DeleteTask("TaskName", false);

var taskDefinition = taskService.NewTask();                        
taskDefinition.RegistrationInfo.Author = WindowsIdentity.GetCurrent().Name;

taskDefinition.RegistrationInfo.Description = "Runs Task with arguments: " + arguments;
taskDefinition.Principal.LogonType = TaskLogonType.InteractiveTokenOrPassword;

var action = new ExecAction(path, arguments);
taskDefinition.Actions.Add(action);
taskService.RootFolder.RegisterTaskDefinition("TaskName", taskDefinition, TaskCreation.Create, "domain\\user", password, TaskLogonType.InteractiveTokenOrPassword);

//get task:
var task = taskService.RootFolder.GetTasks().Where(a => a.Name == ("TaskName").FirstOrDefault();
log.Info("Start task " + task.Name + " with arguemtns " + arguments);

try
{
    task.Run();
}
catch (Exception ex)
{
    log.Error("Error starting task in TaskSheduler with message: " + ex.Message);
}
于 2016-06-30T06:21:43.087 回答
0

文件说

使用现有的交互式令牌来运行任务。用户必须使用用户 (S4U) 登录服务登录。使用 S4U 登录时,系统不存储密码,也无法访问网络或加密文件

所以我猜你是否设置了存储密码的复选框并不重要。

应该适用于 TASK_LOGON_S4U 和 TASK_LOGON_SERVICE_ACCOUNT

于 2016-06-28T12:14:00.767 回答