dahall / TaskScheduler

Provides a .NET wrapper for the Windows Task Scheduler. It aggregates the multiple versions, provides an editor and allows for localization.
MIT License
1.21k stars 191 forks source link

Tasks created on Win7 connected to AD uses DOMAIN\User syntax in the task principal information #836

Closed dahall closed 4 years ago

dahall commented 4 years ago

I came across this issue when looking into an issue I have with Author not being set, as described in #659. I just wanted to comment that every task I create with this library on Windows 7 connected to AD uses the DOMAIN\User syntax in the task principal information, which is confirmed when exporting.

This doesn't fix the registration info author being null, I'd hoped it was related. It also doesn't fix new tasks being created with the DOMAIN\Username principal either. Though it sounds like that might be a Microsoft bug?

Originally posted by @Gwindalmir in https://github.com/dahall/TaskScheduler/issues/835#issuecomment-542440839

dahall commented 4 years ago

@Gwindalmir Will you confirm that you register a task on a Windows 7 system for that same system (not remote to another) and that when you export the task you find the Author blank the UserId value in the "DOMAIN\User" format rather than SID format?

dahall commented 4 years ago

@Gwindalmir Also, does this same thing happen if you use the Windows Task Scheduler applet?

dahall commented 4 years ago

I just spun up my Win7 image connected to an domain. I have confirmed that the exported XML stores the UserId as DOMAIN\User and that with the 2.8.16 release of the library, the Principal.Account retrieves the value correctly. The exported XML does store the Author the same way and RegistrationInfo.Author retrieves that value correctly. Please help me understand what you are seeing and how I can replicate it.

Gwindalmir commented 4 years ago

I don't know how to register a task for a remote system (didn't know it was possible), so I don't know how to confirm that.

However I've attached a screenshot from the task scheduler windows app, and the two tasks created. I did edit out sensitive information though, I hope that's not a problem. I can send privately if needed.

MMC console task created in library.xml.txt task created in scheduler applet.xml.txt

The scheduler service is created like this:

m_service = new TaskService();

A new task is then created like this:

// Create Windows task definition
var def = m_service.NewTask();

var action = (ComHandlerAction)def.Actions.AddNew(TaskActionType.ComHandler);

// Get GUID for handler from class attribute
var guid = (GuidAttribute)typeof(TaskRunner).GetCustomAttributes(typeof(GuidAttribute), false)[0];
action.ClassId = new Guid(guid.Value);

var folder = m_service.GetFolder(TaskFolder);

// Create parent folder if it doesn't exist yet
if (folder == null)
    folder = m_service.RootFolder.CreateFolder(TaskFolder);

// Register new task with Windows
task.Task = folder.RegisterTaskDefinition("task name", def);
return task;
Gwindalmir commented 4 years ago

Oh, and Run as Admin doesn't have an effect on the Author field, though it does affect the SID in Task.SecurityDescriptor.Owner, which (obviously) determines who can edit it.

dahall commented 4 years ago

I see. The Author information is populated via the TaskDefinition.RegistrationInfo.Author property. You can set this to any string you wish. The applet sets the current user's name there automatically.

Gwindalmir commented 4 years ago

Ah, I thought it would have had a default set. I can set it then. Thanks for looking into it!