/// <summary>
        /// Registers (creates) a task in a specified location using a <see cref="TaskDefinition" /> instance to define a 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="definition">The <see cref="TaskDefinition" /> of the registered 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. This will return <c>null</c> if <paramref name="createType"/> is set to <c>ValidateOnly</c> and there are no validation errors.
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Path;Task names may not include any characters which are invalid for file names.
        /// or
        /// Path;Task names ending with a period followed by three or fewer characters cannot be retrieved due to a bug in the native library.
        /// </exception>
        /// <exception cref="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.</exception>
        public Task RegisterTaskDefinition(string Path, TaskDefinition definition, TaskCreation createType, string UserId, string password = null, TaskLogonType LogonType = TaskLogonType.S4U, string sddl = null)
        {
            if (v2Folder != null)
            {
                definition.Actions.ConvertUnsupportedActions();
                var iRegTask = v2Folder.RegisterTaskDefinition(Path, definition.v2Def, (int)createType, UserId, password, LogonType, sddl);
                if (createType == TaskCreation.ValidateOnly && iRegTask == null)
                {
                    return(null);
                }
                return(Task.CreateTask(this.TaskService, iRegTask));
            }

            // Check for V1 invalid task names
            string invChars = System.Text.RegularExpressions.Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars()));

            if (System.Text.RegularExpressions.Regex.IsMatch(Path, @"[" + invChars + @"]"))
            {
                throw new ArgumentOutOfRangeException("Path", "Task names may not include any characters which are invalid for file names.");
            }
            if (System.Text.RegularExpressions.Regex.IsMatch(Path, @"\.[^" + invChars + @"]{0,3}\z"))
            {
                throw new ArgumentOutOfRangeException("Path", "Task names ending with a period followed by three or fewer characters cannot be retrieved due to a bug in the native library.");
            }

            // Adds ability to set a password for a V1 task. Provided by Arcao.
            V1Interop.TaskFlags flags = definition.v1Task.GetFlags();
            if (LogonType == TaskLogonType.InteractiveTokenOrPassword && string.IsNullOrEmpty(password))
            {
                LogonType = TaskLogonType.InteractiveToken;
            }
            if (string.IsNullOrEmpty(UserId))
            {
                UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            }
            switch (LogonType)
            {
            case TaskLogonType.Group:
            case TaskLogonType.S4U:
            case TaskLogonType.None:
                throw new NotV1SupportedException("This LogonType is not supported on Task Scheduler 1.0.");

            case TaskLogonType.InteractiveToken:
                flags |= (V1Interop.TaskFlags.RunOnlyIfLoggedOn | V1Interop.TaskFlags.Interactive);
                if (String.IsNullOrEmpty(UserId))
                {
                    UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                }
                definition.v1Task.SetAccountInformation(UserId, IntPtr.Zero);
                break;

            case TaskLogonType.ServiceAccount:
                flags &= ~(V1Interop.TaskFlags.Interactive | V1Interop.TaskFlags.RunOnlyIfLoggedOn);
                definition.v1Task.SetAccountInformation((String.IsNullOrEmpty(UserId) || UserId.Equals("SYSTEM", StringComparison.CurrentCultureIgnoreCase)) ? String.Empty : UserId, IntPtr.Zero);
                break;

            case TaskLogonType.InteractiveTokenOrPassword:
                flags |= V1Interop.TaskFlags.Interactive;
                using (V1Interop.CoTaskMemString cpwd = new V1Interop.CoTaskMemString(password))
                    definition.v1Task.SetAccountInformation(UserId, cpwd.DangerousGetHandle());
                break;

            case TaskLogonType.Password:
                using (V1Interop.CoTaskMemString cpwd = new V1Interop.CoTaskMemString(password))
                    definition.v1Task.SetAccountInformation(UserId, cpwd.DangerousGetHandle());
                break;

            default:
                break;
            }
            definition.v1Task.SetFlags(flags);

            switch (createType)
            {
            case TaskCreation.Create:
            case TaskCreation.CreateOrUpdate:
            case TaskCreation.Disable:
            case TaskCreation.Update:
                if (createType == TaskCreation.Disable)
                {
                    definition.Settings.Enabled = false;
                }
                definition.V1Save(Path);
                break;

            case TaskCreation.DontAddPrincipalAce:
                throw new NotV1SupportedException("Security settings are not available on Task Scheduler 1.0.");

            case TaskCreation.IgnoreRegistrationTriggers:
                throw new NotV1SupportedException("Registration triggers are not available on Task Scheduler 1.0.");

            case TaskCreation.ValidateOnly:
                throw new NotV1SupportedException("Xml validation not available on Task Scheduler 1.0.");

            default:
                break;
            }
            return(new Task(this.TaskService, definition.v1Task));
        }
        /// <summary>
        /// Registers (creates) a task in a specified location using a <see cref="TaskDefinition"/> instance to define a 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="definition">The <see cref="TaskDefinition"/> of the registered 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 RegisterTaskDefinition(string Path, TaskDefinition definition, TaskCreation createType, string UserId, string password, TaskLogonType LogonType, string sddl)
        {
            if (v2Folder != null)
                return new Task(this.TaskService, v2Folder.RegisterTaskDefinition(Path, definition.v2Def, (int)createType, UserId, password, LogonType, sddl));

            // Adds ability to set a password for a V1 task. Provided by Arcao.
            V1Interop.TaskFlags flags = definition.v1Task.GetFlags();
            switch (LogonType)
            {
                case TaskLogonType.Group:
                case TaskLogonType.S4U:
                case TaskLogonType.None:
                    throw new NotV1SupportedException("This LogonType is not supported on Task Scheduler 1.0.");
                case TaskLogonType.InteractiveToken:
                    flags |= (V1Interop.TaskFlags.RunOnlyIfLoggedOn | V1Interop.TaskFlags.Interactive);
                    if (String.IsNullOrEmpty(UserId))
                        UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                    definition.v1Task.SetAccountInformation(UserId, IntPtr.Zero);
                    break;
                case TaskLogonType.ServiceAccount:
                    flags &= ~(V1Interop.TaskFlags.Interactive | V1Interop.TaskFlags.RunOnlyIfLoggedOn);
                    definition.v1Task.SetAccountInformation(String.IsNullOrEmpty(UserId) ? String.Empty : UserId, IntPtr.Zero);
                    break;
                case TaskLogonType.InteractiveTokenOrPassword:
                    flags |= V1Interop.TaskFlags.Interactive;
                    using (V1Interop.CoTaskMemString cpwd = new V1Interop.CoTaskMemString(password))
                        definition.v1Task.SetAccountInformation(UserId, cpwd.DangerousGetHandle());
                    break;
                case TaskLogonType.Password:
                    using (V1Interop.CoTaskMemString cpwd = new V1Interop.CoTaskMemString(password))
                        definition.v1Task.SetAccountInformation(UserId, cpwd.DangerousGetHandle());
                    break;
                default:
                    break;
            }
            definition.v1Task.SetFlags(flags);

            switch (createType)
            {
                case TaskCreation.Create:
                case TaskCreation.CreateOrUpdate:
                case TaskCreation.Disable:
                case TaskCreation.Update:
                    if (createType == TaskCreation.Disable)
                        definition.Settings.Enabled = false;
                    definition.V1Save(Path);
                    break;
                case TaskCreation.DontAddPrincipalAce:
                    throw new NotV1SupportedException("Security settings are not available on Task Scheduler 1.0.");
                case TaskCreation.IgnoreRegistrationTriggers:
                    throw new NotV1SupportedException("Registration triggers are not available on Task Scheduler 1.0.");
                case TaskCreation.ValidateOnly:
                    throw new NotV1SupportedException("Xml validation not available on Task Scheduler 1.0.");
                default:
                    break;
            }
            return new Task(this.TaskService, definition.v1Task);
        }
Example #3
0
        /// <summary>
        /// Registers (creates) a task in a specified location using a <see cref="TaskDefinition" /> instance to define a 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="definition">The <see cref="TaskDefinition" /> of the registered 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>
        /// <exception cref="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.
        /// </exception>
        public Task RegisterTaskDefinition(string Path, TaskDefinition definition, TaskCreation createType, string UserId, string password = null, TaskLogonType LogonType = TaskLogonType.S4U, string sddl = null)
        {
            if (v2Folder != null)
            {
                return(new Task(this.TaskService, v2Folder.RegisterTaskDefinition(Path, definition.v2Def, (int)createType, UserId, password, LogonType, sddl)));
            }

            // Adds ability to set a password for a V1 task. Provided by Arcao.
            V1Interop.TaskFlags flags = definition.v1Task.GetFlags();
            if (LogonType == TaskLogonType.InteractiveTokenOrPassword && string.IsNullOrEmpty(password))
            {
                LogonType = TaskLogonType.InteractiveToken;
            }
            if (string.IsNullOrEmpty(UserId))
            {
                UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            }
            switch (LogonType)
            {
            case TaskLogonType.Group:
            case TaskLogonType.S4U:
            case TaskLogonType.None:
                throw new NotV1SupportedException("This LogonType is not supported on Task Scheduler 1.0.");

            case TaskLogonType.InteractiveToken:
                flags |= (V1Interop.TaskFlags.RunOnlyIfLoggedOn | V1Interop.TaskFlags.Interactive);
                if (String.IsNullOrEmpty(UserId))
                {
                    UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                }
                definition.v1Task.SetAccountInformation(UserId, IntPtr.Zero);
                break;

            case TaskLogonType.ServiceAccount:
                flags &= ~(V1Interop.TaskFlags.Interactive | V1Interop.TaskFlags.RunOnlyIfLoggedOn);
                definition.v1Task.SetAccountInformation((String.IsNullOrEmpty(UserId) || UserId.Equals("SYSTEM", StringComparison.CurrentCultureIgnoreCase)) ? String.Empty : UserId, IntPtr.Zero);
                break;

            case TaskLogonType.InteractiveTokenOrPassword:
                flags |= V1Interop.TaskFlags.Interactive;
                using (V1Interop.CoTaskMemString cpwd = new V1Interop.CoTaskMemString(password))
                    definition.v1Task.SetAccountInformation(UserId, cpwd.DangerousGetHandle());
                break;

            case TaskLogonType.Password:
                using (V1Interop.CoTaskMemString cpwd = new V1Interop.CoTaskMemString(password))
                    definition.v1Task.SetAccountInformation(UserId, cpwd.DangerousGetHandle());
                break;

            default:
                break;
            }
            definition.v1Task.SetFlags(flags);

            switch (createType)
            {
            case TaskCreation.Create:
            case TaskCreation.CreateOrUpdate:
            case TaskCreation.Disable:
            case TaskCreation.Update:
                if (createType == TaskCreation.Disable)
                {
                    definition.Settings.Enabled = false;
                }
                definition.V1Save(Path);
                break;

            case TaskCreation.DontAddPrincipalAce:
                throw new NotV1SupportedException("Security settings are not available on Task Scheduler 1.0.");

            case TaskCreation.IgnoreRegistrationTriggers:
                throw new NotV1SupportedException("Registration triggers are not available on Task Scheduler 1.0.");

            case TaskCreation.ValidateOnly:
                throw new NotV1SupportedException("Xml validation not available on Task Scheduler 1.0.");

            default:
                break;
            }
            return(new Task(this.TaskService, definition.v1Task));
        }