/// <summary> /// Registers (creates) a new task in the folder using XML to define the task. /// </summary> /// <param name="path">The task name. If this value is NULL, the task will be registered in the root task folder and the task name will be a GUID value that is created by the Task Scheduler service. A task name cannot begin or end with a space character. The '.' character cannot be used to specify the current task folder and the '..' characters cannot be used to specify the parent task folder in the path.</param> /// <param name="xmlText">An XML-formatted definition of the task.</param> /// <param name="createType">A union of <see cref="TaskCreation"/> flags.</param> /// <param name="userId">The user credentials used to register the task.</param> /// <param name="password">The password for the userId used to register the task.</param> /// <param name="logonType">A <see cref="TaskLogonType"/> value that defines what logon technique is used to run the registered task.</param> /// <param name="sddl">The security descriptor associated with the registered task. You can specify the access control list (ACL) in the security descriptor for a task in order to allow or deny certain users and groups access to a task.</param> /// <returns>A <see cref="Task"/> instance that represents the new task.</returns> /// <example><code lang="cs"><![CDATA[ /// // Define a basic task in XML /// var xml = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + /// "<Task version=\"1.2\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">" + /// " <Principals>" + /// " <Principal id=\"Author\">" + /// " <UserId>S-1-5-18</UserId>" + /// " </Principal>" + /// " </Principals>" + /// " <Triggers>" + /// " <CalendarTrigger>" + /// " <StartBoundary>2017-09-04T14:04:03</StartBoundary>" + /// " <ScheduleByDay />" + /// " </CalendarTrigger>" + /// " </Triggers>" + /// " <Actions Context=\"Author\">" + /// " <Exec>" + /// " <Command>cmd</Command>" + /// " </Exec>" + /// " </Actions>" + /// "</Task>"; /// // Register the task in the root folder of the local machine using the SYSTEM account defined in XML /// TaskService.Instance.RootFolder.RegisterTaskDefinition("Test", xml); /// ]]></code></example> public Task RegisterTask(string path, [NotNull] string xmlText, TaskCreation createType = TaskCreation.CreateOrUpdate, string userId = null, string password = null, TaskLogonType logonType = TaskLogonType.S4U, string sddl = null) { if (v2Folder != null) { return(Task.CreateTask(TaskService, v2Folder.RegisterTask(path, xmlText, (int)createType, userId, password, logonType, sddl))); } TaskDefinition td = TaskService.NewTask(); XmlSerializationHelper.ReadObjectFromXmlText(xmlText, td); return(RegisterTaskDefinition(path, td, createType, userId ?? td.Principal.ToString(), password, logonType == TaskLogonType.S4U ? td.Principal.LogonType : logonType, sddl)); }
/// <summary> /// Registers (creates) a new task in the folder using XML to define the task. /// </summary> /// <param name="Path">The task name. If this value is NULL, the task will be registered in the root task folder and the task name will be a GUID value that is created by the Task Scheduler service. A task name cannot begin or end with a space character. The '.' character cannot be used to specify the current task folder and the '..' characters cannot be used to specify the parent task folder in the path.</param> /// <param name="XmlText">An XML-formatted definition of the task.</param> /// <param name="createType">A union of <see cref="TaskCreation"/> flags.</param> /// <param name="UserId">The user credentials used to register the task.</param> /// <param name="password">The password for the userId used to register the task.</param> /// <param name="LogonType">A <see cref="TaskLogonType"/> value that defines what logon technique is used to run the registered task.</param> /// <param name="sddl">The security descriptor associated with the registered task. You can specify the access control list (ACL) in the security descriptor for a task in order to allow or deny certain users and groups access to a task.</param> /// <returns>A <see cref="Task"/> instance that represents the new task.</returns> public Task RegisterTask(string Path, string XmlText, TaskCreation createType = TaskCreation.CreateOrUpdate, string UserId = null, string password = null, TaskLogonType LogonType = TaskLogonType.S4U, string sddl = null) { if (v2Folder != null) { return(Task.CreateTask(this.TaskService, v2Folder.RegisterTask(Path, XmlText, (int)createType, UserId, password, LogonType, sddl))); } try { TaskDefinition td = this.TaskService.NewTask(); XmlSerializationHelper.ReadObjectFromXmlText(XmlText, td); return(this.RegisterTaskDefinition(Path, td, createType, UserId == null ? td.Principal.ToString() : UserId, password, LogonType == TaskLogonType.S4U ? td.Principal.LogonType : LogonType, sddl)); } catch { throw; // new NotV1SupportedException(); } }