如何在C#中使用TaskScheduler设置“仅在登录时运行"和“运行为"? [英] How to set “run only if logged in” and “run as” with TaskScheduler in C#?

查看:35
本文介绍了如何在C#中使用TaskScheduler设置“仅在登录时运行"和“运行为"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c#Task Scheduler托管包装程序以编程方式在Windows系统上生成计划任务.我可以生成任务,但是只有在登录帐户后才能运行它:

I am trying to use the c# Task Scheduler Managed Wrapper to programmatically generate scheduled tasks on a windows system. I can generate tasks, but i cannot get it to run only when the account is logged on:

我一直在环顾四周,我发现了去年提出的另一个SO问题,但是或者没有提到其他相关设置,或者自那时以来代码库中的某些内容发生了变化:

I have been looking around and I found another SO question that was asked last year, but either there are other relevant settings that aren't mentioned, or something in the code base has changed since then:

如何设置仅在登录后才运行"并运行为"在C#中使用TaskScheduler?

我认为这种方法可能是正确的,但是当我尝试该方法时,会收到一个令人困惑的错误消息:

I think this approach is probably correct, but when I try it I get a confusing error message:

Task Scheduler 2.0(1.2)不支持设置此属性.您必须使用InteractiveToken才能使任务在当前用户会话中运行.

Task Scheduler 2.0 (1.2) does not support setting this property. You must use an InteractiveToken in order to have the task run in the current user session.

我正在使用的代码如下:

The code I am using is as follows:

    public static void ScheduleTask(string machineName, string taskName, string taskAccount, string password)
    {
        using (TaskService ts = new TaskService(machineName))
        {
            TaskDefinition td = ts.NewTask();

            td.Principal.RunLevel = TaskRunLevel.Highest;
            td.Principal.UserId = WindowsIdentity.GetCurrent().Name;
            td.Principal.LogonType = TaskLogonType.InteractiveToken;

            td.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
            td.Settings.DisallowStartIfOnBatteries = false;
            td.Settings.StopIfGoingOnBatteries = false;
            td.Settings.StartWhenAvailable = true;
            //td.Settings.RunOnlyIfLoggedOn = true;
            td.Settings.Enabled = true;
            td.Settings.Hidden = false;
            td.Settings.AllowHardTerminate = true;
            td.Settings.ExecutionTimeLimit = new TimeSpan();

            var tt = new SessionStateChangeTrigger();
            tt.StartBoundary = DateTime.Now.AddMinutes(1);
            tt.UserId = taskAccount;
            tt.StateChange = TaskSessionStateChangeType.RemoteConnect;
            tt.Repetition.Interval = TimeSpan.FromMinutes(1);
            tt.Repetition.StopAtDurationEnd = false;
            td.Triggers.Add(tt);

            td.Actions.Add("notepad.exe", "c:\\test.log");

            ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, taskAccount, password, TaskLogonType.Password, null);
        }
    }

如果我使用有效的服务器,用户等运行此代码,它将生成一个没有问题的任务.如果我在"RunOnlyIfLogLogOn"参数中添加注释,它将生成我先前提到的错误.请注意,我将LogonType属性设置为TaskLogonType.InteractiveToken,因此必须缺少其他内容.

If I run this code with a valid server,user, etc. it generates a task w/o a problem. If I comment in the 'RunOnlyIfLoggedOn' parameter, it generates the error I previously mentioned. Note that I am setting the LogonType property to TaskLogonType.InteractiveToken, so there must be something else that I am missing.

推荐答案

好的,有解决方案:

注册任务定义调用需要将其TaskLogonType设置为交互式令牌".仅仅将TaskDefinition Principal Logon类型设置为使用交互式令牌是行不通的.

The register task definition call needs to have its TaskLogonType set to Interactive Token. Just setting the TaskDefinition Principal Logon type to use an interactive Token will not work.

RunOnlyIfLoggedOn似乎仅适用于任务调度程序的早期版本(v1,在w2k3等系统上)

RunOnlyIfLoggedOn appears to only work for earlier versions of the task scheduler (v1, on systems like w2k3, etc)

这篇关于如何在C#中使用TaskScheduler设置“仅在登录时运行"和“运行为"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆