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.
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.