This project has moved. For the latest updates, please go here.

Failure in creating tasks

Sep 21, 2011 at 3:03 PM

Hi,

I'm having a strange issue wherein the tasks I attempt to create isn't getting created and listed in the Scheduled Tasks console. The error that I get is as follows.

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))   at Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.RegisterTaskDefinition(String Path, ITaskDefinition pDefinition, Int32 flags, Object UserId, Object password, TaskLogonType LogonType, Object sddl)   at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl)

The following is the code I'm using to create the task.

using ( TaskService service = new TaskService( ) )
            {
                ExecAction action = new ExecAction( @"c:\windows\notepad.exe" );

                TimeTrigger trigger = new TimeTrigger( );
                trigger.StartBoundary = DateTime.Now.AddMinutes( 2 );
                trigger.EndBoundary = trigger.StartBoundary.AddHours( 12 );

                TaskDefinition definition = service.NewTask( );
                definition.RegistrationInfo.Description = txtTaskName.Text;
                definition.Actions.Add( action );
                definition.Settings.AllowHardTerminate = true;
                definition.Principal.RunLevel = TaskRunLevel.Highest;
                definition.Triggers.Add( trigger );

                try
                {
                    service.RootFolder.RegisterTaskDefinition( txtTaskName.Text, definition, TaskCreation.CreateOrUpdate, txtUserName.Text, txtPassword.Text, TaskLogonType.Password );
                    txtResponse.Text = string.Format( "Task: {0} registered successfully.", txtTaskName.Text );
                }
                catch ( Exception ex )
                {

                    txtResponse.Text = ex.Message + Environment.NewLine + ex.StackTrace;
                }

            }

I'm able to manually create the task using the console but not programmatically. 

I'm using Windows Server 2008 R2 Standard, .NET Framework 4.0 and v.1.6.4186.14621 of Microsoft.Win32.TaskScheduler.dll

Can anyone please give me a clue as to what must be going wrong here? Is it something to do with CAS settings in the machine or anything else?

Thanks & Regards,

Sanjay.

Coordinator
Sep 21, 2011 at 6:34 PM

My guess is that it has to do with the privileges of the account you are using to run the program. That account has to have the rights to create tasks. You have two ways to specify an account. When you create a TaskService instance with no parameters, the account running the executable is used. If you are launching this from a user session, make sure you are running as Administrator so UAC doesn’t choke the privileges. You can also instantiate TaskService specifying the username, password and domain. This way ensures that you are running with known privileges.

Oct 12, 2011 at 10:58 AM

Hi Dahall,

Thank you so much for your guidance. The code was as per your suggestion. But what I could find is that it's some sort a permissions issue in this particular server. I also tried impersonating the task creation

code in an impersonation context and calling the default ctor of TaskService using an account that's a member of the Administrators group. It still fails. But if the application is set to "Run as Administrator"

or set to run in XP SP3 compatibility mode, then it works fine. I suppose it's something to do with User Access Control. UAC seems to be disabled on the server though. What are your picks on this and what

do you think must be happening? 

Thank you once again for all the great suggestions.

Regards,

Sanjay.

Coordinator
Oct 12, 2011 at 8:46 PM

You don't need to use impersonation when connecting. Use the additional parameters on the TaskService constructor to specify the username, domain, and password of the account that has the correct privileges. Then you should be able to register the task without error.

Oct 13, 2011 at 12:10 PM

Hi,

Am having the same issue, and understand that we require admin rights to create the task, if I run my program as a normal user, i get the access is denied exception as above, if I run as adminstrator the task is created fine. (I am using TaskService with no paramaters)

My question is though why can I successfully create a task as the normal user, through the windows task console, using the same settings as I am using programmatically? I would expect it to have the same behaviour as my program and not be allowed. Does the Windows task console run with escalated priveleges without me knowing it? (I'm on Win 7 so would expect the UAC pop up if it was doing this).

Thanks in advance if anyone can advise.

Andy

Coordinator
Oct 13, 2011 at 9:58 PM

From what I have read about UAC, yes, all MMC actions run with elevated privileges. I found an article here that shows how to request elevated privileges for your .NET application. Let me know if this helps.