Delete task permissions issues

Topics: Errors, Examples
Mar 30 at 6:45 PM
Edited Mar 30 at 6:46 PM

I was recently trying to figure out a permissions problem that cropped up when I tried to delete a scheduled task. I was working under an account that was a member of the administrator group, and I could create and update tasks without a problem, but when I tried to delete them I got the standard perms error: 'Delete Task HRESULT: 0x80070005 (E_ACCESSDENIED))'

Running the task (or visual studio when debugging) as an administrator corrected the problem, but I think that you might want to update your sample delete task code (or even the underlying library wrapper) to be a little more aware of the user's current permissions before trying a futile task:

void DeleteTask(taskName)
// A taskName by itself assumes the root folder (e.g. "MyTask")
// A taskName can include folders (e.g. "MyFolder\MySubFolder\MyTask")
Task t = ts.GetTask(taskName);
if (t == null) return;

WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
string account_name = WindowsIdentity.GetCurrent().Name;
throw new Exception($"Cannot delete scheduled task with your current identity '{account_name}' permissions level. You might need to run this application 'as administrator' even if you are using an administrator account.");

// Remove the task we just created

Warning people that extra permissions are required for task deletion would be a good step over just passing back a win api error message.

Nice work on this library otherwise, though.
Mar 31 at 3:54 PM
Great idea. That information is covered elsewhere in the documentation, but I agree that calling it out will help people newer to task management and the library. I'll make the change using your code today.