Cannot create task in Windows Vista and Windows Server 2003

Sep 29, 2011 at 1:37 AM

Hello everyone,

I create a task in Windows Vista, but when it run to line RegisterTaskDefinition, it cause an exception "Access is denied 0x80070005. I granr user TEST as Administrators but cannot fix it.

The s.HighestSupportedVersion is 1.2 and ts.Connected is true.

Can anyone help me? I spend 2 days to fix it but cannot. Thanks a lot.

using (TaskService ts = new TaskService())
                {
                    TaskDefinition td = ts.NewTask();
                    td.Triggers.Clear();

                    DailyTrigger dailyTrig = new DailyTrigger();
                    dailyTrig.StartBoundary = DateTime.Now;
                    dailyTrig.DaysInterval = 1;
                    td.Triggers.Add(dailyTrig);

                    ExecAction ac = new ExecAction("D:\\a.exe");
                    td.Actions.Add(ac);

                    if (Environment.OSVersion.Version.Major > 5)
                    {
                        td.Principal.RunLevel = TaskRunLevel.Highest;
                    }

                    // Register the task in the root folder
                    ts.RootFolder.RegisterTaskDefinition("TESTTASK", td, TaskCreation.CreateOrUpdate, Environment.MachineName + "\\TEST", "123456", TaskLogonType.Password, null);
                }

 

Coordinator
Sep 29, 2011 at 2:15 PM
Edited Sep 29, 2011 at 8:32 PM

First of all, you have a lot of extra stuff in your code.

  • When creating a new task, you don't need to Clear the triggers as they are already empty
  • The default constructor for DailyTrigger sets the StartBoundary to Now and the DaysInterval to 1. You don't need to do it again.

On your particular problem, what happens when you just call ts.RootFolder.RegisterTaskDefinition("TESTTASK", td) without any user credentials? If that works, you may have a permissions problem with the account. Do you have the same problem if you run from the debugger or from the executable?

If you don't need to supply user credentials, you can do all you're doing in many fewer lines:

using (TaskService ts = new TaskService())
{
   ts.AddTask("Test", new DailyTrigger(), new ExecAction("D:\\a.exe"));
}

 

 

Coordinator
Sep 29, 2011 at 8:33 PM

I would strongly recommend reading the Microsoft documentation at http://msdn.microsoft.com/en-us/library/windows/desktop/aa382140(v=VS.85).aspx for even more information on Security Contexts for Tasks.