"Run with highest privileges" not getting checked

Jul 26, 2011 at 1:58 PM

Hi,

I was trying to create a task on a Windows Server 2008 machine and the task gets created. But I could find that the 'Run with highest privileges' option is unchecked due to which the program that this task is running fails. Is there any condition that needs to be met while creating tasks using this API for this option to be enabled?

The following is the code snippet that I use.

using (TaskService taskSvc = new TaskService(null, null, null, null, true))
                {

                    //initializing the task definition
                    TaskDefinition taskDef = taskSvc.NewTask();

                    taskDef.RegistrationInfo.Description = scheduledTask.ScheduleDescription;
                    taskDef.RegistrationInfo.Author = scheduledTask.ScheduleAuthor;
                    //identification data of a scheduled task
                    taskDef.Data = scheduledTask.ParentEntity.WidgetInstanceID.ToString();
                    

                    //adding appropriate triggers
                    foreach (var trigger in scheduledTask.Triggers)
                        this.ProcessAndAddTrigger(taskDef, trigger);

                    //adding action
                    taskDef.Actions.Add(new ExecAction(actionPath,
                        scheduledTask.ScheduleIID.ToString(),
                        string.Empty));

                    //if the schedule name already exists, then deleting and recreating it
                    this.DeleteSchedule(scheduledTask.ScheduleName);

                    //registering the scheduled task
                    if (!string.IsNullOrEmpty(runUserID))
                        taskSvc.RootFolder.RegisterTaskDefinition(scheduledTask.ScheduleName,
                            taskDef,
                            TaskCreation.CreateOrUpdate,
                            runUserID,
                            runUserPassword,
                            TaskLogonType.Password,
                            null);
                    else
                        taskSvc.RootFolder.RegisterTaskDefinition(scheduledTask.ScheduleName,
                            taskDef,
                            TaskCreation.CreateOrUpdate,
                            null,
                            null,
                            TaskLogonType.ServiceAccount,
                            null);

                    taskDef.Dispose();
Coordinator
Jul 26, 2011 at 9:04 PM

That item is related to the TaskDefinition.Principal.RunLevel property and can be set to LUA or Highest under V2 (Vista and later). Prior to Vista, that value always returns Highest and cannot be set.

Jul 27, 2011 at 5:47 AM

Hi,

Thank you for the speedy reply. I checked the task scheduler version on the Windows Server 2008 machine and it reads Ver. 1.0. I guess, because of this, the RunLevel property I set is throwing an exception as TaskPrincipal.set_RunLevel is not supported on Task Scheduler 1.0. I believe Windows Server 2008 is supposed to have Ver.2.0 of the Task Scheduler if I'm not mistaken. Could you please let me know what's wrong here and a solution if possible?

Thank you.

Coordinator
Jul 27, 2011 at 8:17 PM
Edited Jul 27, 2011 at 8:21 PM

Window Server 2008 should have version 2 of the Task Scheduler. Make sure that when you construct the TaskService instance, that you are not forcing version 1.0 behavior by specifying 'true' as the value to the last parameter as you have in your sample code above. By specifying 'false', you will allow for the highest version of the Task Scheduler to be used. You can then use the TaskService.HighestSupportedVersion property to determine which version of the library you are using. That property will return Version(1,1) when under version 1 and Version(1,2) or higher when under version 2.

Jul 29, 2011 at 6:03 AM

Great.. Thank you Dahall.

On further investigation, I could figure out that the option was remaining unchecked because of UAC settings on that machine. When I disabled UAC, the option was getting checked even with v1.0 compatibility. Anyway, thank you for you help.

 

Dec 19, 2011 at 2:37 PM

I am currently trying that on Win7.

I find no way to enable the "Highest Privileges". It just stays disabled :(.

 

td.Principal.RunLevel = TaskRunLevel.Highest;

MessageBox.Show(ts.HighestSupportedVersion.ToString()); //1.3

Coordinator
Dec 19, 2011 at 5:39 PM

This property works under Windows 7 when you have constructed the TaskService instance or registered the task with an account that has permissions to run things at that run level. You also need to make sure the executable is running with UAC on as noted above.

Dec 19, 2011 at 6:05 PM

I have run the application with right click/as administrator.

But it won't take it. :-/

 

Any idea?

 

Coordinator
Dec 19, 2011 at 9:06 PM

Will you try just something simple like the following and see if it works?

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[] args)
   {
      // Get the service on the local machine
      using (TaskService ts = new TaskService())
      {
         // Create a new task definition and assign properties
         TaskDefinition td = ts.NewTask();
         td.Principal.RunLevel = TaskRunLevel.Highest;

         // Create a trigger that will fire the task at this time every other day
         td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });

         // Create an action that will launch Notepad whenever the trigger fires
         td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

         // Register the task in the root folder
         ts.RootFolder.RegisterTaskDefinition(@"Test", td);
      }
   }
}
Dec 20, 2011 at 6:27 PM

Got it ;) I wrote the RunLevel thing under the RegisterTaskDefinition :D.

I have used it in my following project, to reset my network interfaces to dhcp on each wakeup of the system.

http://forum.scilor.com/viewtopic.php?t=947&p=2799#p2799

Oct 10, 2012 at 3:51 PM

The code posted by dahall works fine. To see the option as "selected", you have to reload Task Scheduler window by pressing F5. Switching from an activity row to another, the window keep showing the old settings...

Sorry for the late response! :) Bye!