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

Delete task permissions issues

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

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
ts.RootFolder.DeleteTask(taskName);
}

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.
Coordinator
Mar 31 at 4: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.
Jul 21 at 7:33 AM
dahall wrote:
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.
Tell me please, will the confirmation be added to delete the task without restarting the application with administrator rights?
Coordinator
Jul 24 at 3:23 PM
smiz001: I'm not sure what you're asking exactly. There is not a mechanism to override the system if it determines that permissions from the calling application are not sufficient to perform an operation. That is why it throws the exception. This request was to further explain the exception so that developers can better react to the root cause of the problem.