3

The app I'm participating adds tasks to Windows Task Sheduler. It works fine on Windows 7, but on Win 8.1 and Win 10 the access is denied unless "run as admin" is used.

The app must be launched with "run as admin" permissions in order to create the scheduled task.

I cannot share the code, because it doesn't belong to me. Anyway, maybe someone can answer it.

My gues is that in Windows 8.1 (and later) the permission level for scheduled tasks changed and Administrative permissions are required. But I cannot find a proof of it and any possible alternatives to programmatically add Scheduled Tasks in Windows 10 without admin rights.

The code uses Microsoft.Win32.TaskScheduler lib. The code below is simplified to show the way it's written and classes/parameters are used.

    var taskDefinition = Microsoft.Win32.TaskScheduler.TaskService.NewTask();
    taskDefinition.Principal.LogonType = Microsoft.Win32.TaskScheduler.TaskLogonType.S4U; 
    taskDefinition.Principal.UserId = "";

then it adds the action and trigger

    taskDefinition.Actions.Add(executeAction);
    taskDefinition.Triggers.Add(trigger);

then register the task

    Microsoft.Win32.TaskScheduler.TaskService.RootFolder.RegisterTaskDefinition(taskName, taskDefinition);
Sergey
  • 113
  • 12
  • Obviously the code that creates the task uses public classes and methods from the framework. You need to at least tell us how it is trying to create those tasks. List the classes and methods being called and how they are called. –  Jun 22 '17 at 14:03
  • Added simplified code in the question – Sergey Jun 23 '17 at 10:24

1 Answers1

1

As the author of that library, I can confirm that the permission context changed in Windows 8. However, you're on the right track. The library helps you here to simplify things. Try the following, it should work on all Windows versions and regardless of whether the codes runs elevated or not:

var trigger = new DailyTrigger(1);
var action = new ExecAction(exePath);
TaskService.Instance.AddTask(taskName, trigger, action);
dahall
  • 318
  • 3
  • 6