简体   繁体   English

如何使用“运行用户是否登录”在任务计划中创建任务

[英]How to create a task in task schedular using “Run whether the user is logged on or not”

And also tell me about "Run whether user is logged on or not" in detail. 并且还详细告诉我“运行用户是否登录”。 to avoid future hurdles to run the created task(details about username and password) 避免将来遇到障碍以运行创建的任务(有关用户名和密码的详细信息)

To create a task in taskscheduler with the setting: " run wether user is loggd on or not " use following code: 要在taskscheduler中使用以下设置创建任务:“ run wether user is loggd on not not ”使用以下代码:

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

                    taskDefinition.RegistrationInfo.Description = "Runs Application";

                    // 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("NameOfApplication", taskDefinition);

Note: I dont work with a Trigger here. 注意:我不在这里使用Trigger。 You can start the created task directly from code with following code: 您可以使用以下代码直接从代码启动创建的任务:

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

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

You can set the UserId to SYSTEM and it will automatically set the option 'Run whether user is logged on or not'. 您可以将UserId设置为SYSTEM ,它将自动设置“运行用户是否登录”选项。

using (TaskService ts = new TaskService())
{
    var newTask = ts.NewTask();
    newTask.Principal.UserId = "SYSTEM";
    newTask.Triggers.Add(new TimeTrigger(DateTime.Now));
    newTask.Actions.Add(new ExecAction("notepad"));
    ts.RootFolder.RegisterTaskDefinition("NewTask", newTask);
}

The program executing the above code must run as Administrator. 执行上述代码的程序必须以管理员身份运行。

Found this solution posted in the comments here, http://taskscheduler.codeplex.com/wikipage?title=Examples 发现此解决方案发布在这里的评论中, http://taskscheduler.codeplex.com/wikipage?title = Examples

ITaskFolder rootFolder = taskService.GetFolder(@"\");
rootFolder.RegisterTaskDefinition(taskName, 
                                  taskDefinition,
                                  (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
                                  null,
                                  null, 
                                  _TASK_LOGON_TYPE.TASK_LOGON_S4U,
                                  null);

I just try to use all of _TASK_LOGON_TYPE and found that "TASK_LOGON_S4U" work for setting Run whether user is logged on or not . 我只是尝试使用_TASK_LOGON_TYPE全部,并发现“TASK_LOGON_S4U”用于设置Run是否登录用户 Details about TaskScheduler http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx 有关TaskScheduler的详细信息,请访问http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM