1

I'm creating a Windows Scheduled Task from a MVC app # by using the c# TaskService namespace.

It normally works fine if the IIS AppPool is set to run as the LocalService user, but every now and then we find a server where it fails to create the Scheduled Task. If we change the AppPool user to a local (admin) user, then it works. But I would prefer to run the AppPool under a system user of some kind.

I don't have any specific error message yet as I can't replicate the error myself.

Are there any rigths/settings/permissions that can cause the LocalService user not to be able to create the Scheduled Tasks?

(PS: LocalSystem and the IIS AppPool users also does not work)

using (TaskService ts = new TaskService())
{
    TaskDefinition td = ts.NewTask();
    td.RegistrationInfo.Description = "My Task";

    var trigger = new RegistrationTrigger() { Delay = TimeSpan.FromSeconds(1) };
    trigger.Repetition.Interval = new TimeSpan(1, 0, 0));
    td.Triggers.Add(trigger);

    td.Actions.Add(new ExecAction("C:\Folder\program.exe"));
    td.Settings.AllowDemandStart = task.AllowDemandStart;
    td.Settings.Hidden = task.Hidden;
    ts.RootFolder.RegisterTaskDefinition("My Task", td);  //Assumption is that error happens on this line
}
Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
  • Can you check the Windows event logs to see if there are any messages relating to the failure there? – NightOwl888 Jan 23 '18 at 11:09
  • Nothing in the Win Event Log – Cameron Castillo Jan 23 '18 at 11:56
  • Please post your code – Matt Evans Jan 29 '18 at 14:19
  • Does it always fail on the servers in question, or just occassionally? – Matt Evans Jan 29 '18 at 14:22
  • It always fails with LocalService, and always works with a local Admin user. – Cameron Castillo Jan 29 '18 at 18:18
  • Can you give a bit more information on where this is going to run? Right now it looks like you're running this from an MVC webapplication. I wouldn't give the user under which the apppool is running rights like this. It would be better to run this apart from iis and create a new user to run this task. – Bunnynut Jan 30 '18 at 14:18
  • Yes, the scheduled task will be created from an MVC application. As there are parameters that the user can configure we feel the need to create the task from our web app (i.e. from IIS). This will happen on 100's of servers so to do that manually will be a big work load. – Cameron Castillo Jan 30 '18 at 15:00

1 Answers1

0

I cannot close this as duplicate because it has a bounty; but for reference this has been answered here

Permissions required to create/modify tasks in Windows Task Scheduler

"The account needs to have read/write permissions to the "Tasks" directory. Here's the path: %SystemRoot%\system32\Tasks\

However, there was an additional problem where Windows Server 2003 didn't like me specifying Local System as the account that the task would run under. It seemed to not like me passing in a null password which is how you specify Local System. I worked around the problem by making a local account on the server for tasks to run under and specifying this new account."

PhillipH
  • 6,182
  • 1
  • 15
  • 25
  • Thanks. I can't test it at the moment as we don't have access to a problematic server now. But the answer seems pretty accurate and good enough for the bounty. Tx. – Cameron Castillo Feb 05 '18 at 09:43