![]() | Task |
public Task RegisterTaskDefinition( string path, TaskDefinition definition, TaskCreation createType, string userId, string password = null, TaskLogonType logonType = TaskLogonType.S4U, string sddl = null )
Exception | Condition |
---|---|
ArgumentOutOfRangeException | Task names may not include any characters which are invalid for file names. or Task names ending with a period followed by three or fewer characters cannot be retrieved due to a bug in the native library. |
NotV1SupportedException | This LogonType is not supported on Task Scheduler 1.0. or Security settings are not available on Task Scheduler 1.0. or Registration triggers are not available on Task Scheduler 1.0. or XML validation not available on Task Scheduler 1.0. |
This first example registers a simple task with a single trigger and action using the default security.
// Create a new task definition for the local machine and assign properties TaskDefinition td = TaskService.Instance.NewTask(); td.RegistrationInfo.Description = "Does something"; // Add a trigger that, starting tomorrow, will fire every other week on Monday and Saturday td.Triggers.Add(new WeeklyTrigger(DaysOfTheWeek.Monday | DaysOfTheWeek.Saturday, 2)); // Create an action that will launch Notepad whenever the trigger fires td.Actions.Add("notepad.exe", "c:\\test.log"); // Register the task in the root folder of the local machine using the current user and the S4U logon type TaskService.Instance.RootFolder.RegisterTaskDefinition("Test", td);
This example registers that same task using the SYSTEM account.
TaskService.Instance.RootFolder.RegisterTaskDefinition("TaskName", taskDefinition, TaskCreation.CreateOrUpdate, "SYSTEM", null, TaskLogonType.ServiceAccount);
This example registers that same task using a specific username and password along with a security definition.
TaskService.Instance.RootFolder.RegisterTaskDefinition("TaskName", taskDefinition, TaskCreation.CreateOrUpdate, "userDomain\\userName", "userPassword", TaskLogonType.Password, @"O:BAG:DUD:(A;ID;0x1f019f;;;BA)(A;ID;0x1f019f;;;SY)(A;ID;FA;;;BA)(A;;FR;;;BU)");