System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Topics: Errors, Examples
Jul 28, 2015 at 5:33 PM
I have two servers in a domain: SERVER_A and SERVER_B. SERVER_A runs a collection of scheduled tasks defined in the Task Scheduler.

I have an application that creates scheduled tasks by importing an XML task definition file using the Task Scheduler Managed Wrapper using the following code:

Imports System.IO
Imports Microsoft.Win32.TaskScheduler

Dim ts as TaskService
Dim tf As TaskFolder
Dim FolderName as String = "\TaskFolder"
Dim taskDefFilePath As String
Dim taskDefXML As String

ts = New TaskService("SERVER_A")
tf = ts.GetFolder(FolderName)
taskDefXML = IO.File.ReadAllText(taskDefFilePath)

When the above code is run from SERVER_B (connecting remotely to SERVER_A to set up the scheduled task), the code runs as expected and sets up a new scheduled task. If I run the above code from SERVER_A, then the application throws the following exception:

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.RegisterTask(String Path, String XmlText, Int32 flags, Object UserId, Object password, TaskLogonType LogonType, Object sddl)
at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTask(String Path, String XmlText, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl)

I am logging into SERVER_A and SERVER_B using a domain user that has local administrator rights on SERVER_A.

Any idea why I can run the above code remotely from another server in the domain, but get the exception thrown when I try to run locally on SERVER_A?
Nov 11, 2015 at 5:39 PM
Are the two servers running different versions of Windows? Are they in different domains? Could the account of the process running this code have different permissions on each of the two servers?
Nov 12, 2015 at 8:31 PM
Same version of Windows.
Same domain (very first point in the original post).
The account running the process has local admin rights on both servers. The account is a domain account. Technically, the only thing that should matter is that the account has rights to administer the scheduled tasks on the machine where the scheduled tasks are created and run (SERVER_A).

I can use the Scheduled Task Manager directly on SERVER_A; or running Scheduled Task Manager on SERVER_B and remotely administering the scheduled tasks on SERVER_A.
Nov 17, 2015 at 5:26 PM
Have you checked the information on the Security Documentation page? It could have something to do with the firewall settings, UAC, or group membership on the target server.
Nov 17, 2015 at 7:14 PM
Able to run remotely and manage a remote Task Scheduler. Fails when trying to run the app on the same machine as the Task Scheduler.
Able to use the standard Windows Scheduled Task Manager either locally on the same machine where the task is scheduled, or remotely to manage the scheduled tasks from another machine in the domain.

Firewall would not be an issue.
Security would not be an issue.

Security or a firewall might make sense if it worked locally on the target server but not remotely. It's working remotely but not locally - the opposite of what you'd expect if security or some sort of networking issue was the problem.
Nov 18, 2015 at 2:45 PM
And when you run it locally, are you running it "as administrator"? UAC does some crazy stuff and the MMC Task Scheduler app forces itself to run elevated.