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

Unable to create a task

Jun 24, 2010 at 1:09 PM
Edited Jun 24, 2010 at 2:06 PM

Hi.

My users are continuously getting the following exceptions when creating a task from my application :-

  1. System.UnauthorizedAccessException, Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). (User was logged in with Administrator rights.)
  2. System.Runtime.InteropServices.COMException, Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7) (User was creating Task for the 1st time)
  3. System.Runtime.InteropServices.COMException, Unable to establish the "existence of the" specified account. (Exception from HRESULT: 0x80041310) (User was creating a task by passing user credentials to run the task even if he is not logged in - using variable _runWhenUserNotLoggedIn )

Note: The errors mentioned above were send by three different users.

I was trying to simulate this problem on my side, but I'm not able simulate it.  I gone through the code also, but didn't find anything wrong.
I'm sending you my code snippet, please help me to get rid of this problem.

Dim taskService As New TaskService()
Dim taskFolder As TaskFolder = taskService.RootFolder
Dim taskDefinition As TaskDefinition = taskService.NewTask()

taskDefinition.Data = ""

If _runWhenUserNotLoggedIn = False Then
   taskDefinition.Principal.UserId = String.Concat(Environment.UserDomainName, "\", Environment.UserName) 'I thought this statement is causing the problem, but its working fine over my side
   taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken
End If

taskDefinition.RegistrationInfo.Author = STR_Author
taskDefinition.RegistrationInfo.Description = ""
taskDefinition.RegistrationInfo.Documentation = ""
taskDefinition.Settings.DisallowStartIfOnBatteries = True
taskDefinition.Settings.Enabled = Me.Enabled
taskDefinition.Settings.Hidden = False
taskDefinition.Settings.RunOnlyIfIdle = False
taskDefinition.Settings.IdleSettings.IdleDuration = TimeSpan.FromMinutes(20)
taskDefinition.Settings.IdleSettings.WaitTimeout = TimeSpan.FromMinutes(10)
taskDefinition.Settings.IdleSettings.StopOnIdleEnd = False
taskDefinition.Settings.IdleSettings.RestartOnIdle = False
taskDefinition.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal
taskDefinition.Settings.RunOnlyIfNetworkAvailable = False
taskDefinition.Settings.StopIfGoingOnBatteries = True

If newVersion Then
   taskDefinition.Principal.RunLevel = TaskRunLevel.LUA
   taskDefinition.Settings.AllowDemandStart = True
   taskDefinition.Settings.AllowHardTerminate = True
   taskDefinition.Settings.Compatibility = TaskCompatibility.V2
   taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.Zero
   taskDefinition.Settings.MultipleInstances = TaskInstancesPolicy.Parallel
   taskDefinition.Settings.StartWhenAvailable = True
   taskDefinition.Settings.WakeToRun = False
End If

Dim runOnceTrigger As New TimeTrigger
runOnceTrigger.StartBoundary = 'Some date time here
runOnceTrigger.EndBoundary = 'some data time here
taskDefinition.Triggers.Add(runOnceTrigger)

taskDefinition.Actions.Add(New ExecAction("My Application Path", "My Arguments", System.Windows.Forms.Application.StartupPath))

If _runWhenUserNotLoggedIn = False Then
    taskFolder.RegisterTaskDefinition(taskPath, taskDefinition)
Else
    taskFolder.RegisterTaskDefinition(taskPath, taskDefinition, TaskCreation.CreateOrUpdate, String.Concat(Environment.UserDomainName, "\", Environment.UserName), "User Specified Password", TaskLogonType.InteractiveTokenOrPassword, "")
End If


Thank you!

Jul 6, 2010 at 1:34 PM

Hey guys, still I have not found the solution of above mentioned problems.

Now I'm trying to create a task which will run in any user account.  If anybody knows how to do it, then please reply soon.

Thank You!

Coordinator
Jul 7, 2010 at 3:21 PM

These are tough ones to debug, but I will give you my thoughts on each error in hopes that it will help you find these needles in your haystack.

1. System.UnauthorizedAccessException, Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). (User was logged in with Administrator rights.)
>> This could be a problem with the file system. An administrator should always be able to register a task.
2. System.Runtime.InteropServices.COMException, Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7) (User was creating Task for the 1st time)
>> If this was V1, then for sure this is a file system ACL problem on the %WINDOWS%\tasks directory. If V2, then this could be a registry ACL problem.
3. System.Runtime.InteropServices.COMException, Unable to establish the "existence of the" specified account. (Exception from HRESULT: 0x80041310) (User was creating a task by passing user credentials to run the task even if he is not logged in - using variable _runWhenUserNotLoggedIn )
>> This just sounds like a system error with the host not being to reach the AD server.

Do you know which OS each of these errors is occuring on? Have you checked the Microsoft KB to see if there are system problems causing these errors?

Aug 8, 2010 at 5:08 PM

I had the 0x80070005 (E_ACCESSDENIED) exception occuring on a Windows Server 2008 machine. The only way that I was able to resolve this was by using impersonation, oddly enough, with the same domain / user / password that I was using in the TaskService constructor, e.g.

this.TaskService = new TaskService(this.ComputerName, this.UserName, this.Domain, this.Password);

did not work but

context = new WrapperImpersonationContext(this.Domain, this.UserName, this.Password);
context.Enter();
this.TaskService = new TaskService(this.ComputerName);

did work. I wanted to share this with users in case they run into this problem. The impersonation context wrapper was taken from http://www.vanotegem.nl/PermaLink,guid,36633846-2eca-40fe-9957-2859d8a244dc.aspx