Sergey
Sergey

Reputation: 113

Programmatically creating scheduled tasks fails on Windows 10 unless "run as admin" is used

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);

Upvotes: 3

Views: 1290

Answers (1)

dahall
dahall

Reputation: 348

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);

Upvotes: 2

Related Questions