This project has moved and is read-only. For the latest updates, please go here.

Updating/Deleting Tasks with "elevated" privileges

Jan 27, 2012 at 4:33 PM

I've been having an issue when updating or deleting existing tasks when UAC is involved. Here are the steps that cause the issue to happen:

  1. I run my application. My application creates (or updates) a task on startup. Initially, the application has no admin privileges. The task is created successfully. If I restart the app, the task is updated and/or deleted successfully as well.
  2. At one point in my app, I have to get admin privileges. After granting admin privileges through UAC, I then create the task again (overwriting the previous one).
  3. Now, if I restart my app (thus, losing admin privileges) and try to update or delete the task again, I get this error:
    1. 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) 

I resolved this for now by preventing the task from being updated or created when admin privileges (via UAC) are set, but it isn't a perfect solution. Is there another way of fixing this?

Jan 27, 2012 at 11:35 PM

If you specify an account always when registering the task, and always use that same account when re-registering it, it will ignore the current user settings.

Jan 30, 2012 at 4:25 PM
Edited Jan 30, 2012 at 4:26 PM

What if I don't know what the user account being used is? Is there a way to get the current logged in user's credentials for registration of a task?

Jan 30, 2012 at 8:23 PM

Not that I know of. I guess my direction would be to follow MS guidelines and release Admin rights as soon as they are no longer needed. Then you won't have to keep track of which type of credentials are used to create a task.

Jan 30, 2012 at 8:54 PM

Unfortunately that isn't possible. Admin rights are needed while my application is running it's processes. I guess, for now, I'll have to just be careful with adding tasks while my app has admin rights. Thank you for the help though!