Beispiel #1
0
        static void addTask()
        {
            // Get the service on the local machine
            using (TaskService ts = new TaskService())
            {
                // Create a new task definition and assign properties
                TaskDefinition newTask = ts.NewTask();
                newTask.RegistrationInfo.Description = "Rondinelli Morais Create Task";

                newTask.Triggers.Add(new LogonTrigger());

                newTask.Actions.Add(new ExecAction("C:\\Windows\\regedit.exe"));

                newTask.Principal.RunLevel  = TaskRunLevel.Highest;
                newTask.Principal.LogonType = TaskLogonType.InteractiveToken;

                newTask.Settings.Compatibility              = TaskCompatibility.V2_1;
                newTask.Settings.AllowDemandStart           = true;
                newTask.Settings.DisallowStartIfOnBatteries = false;
                newTask.Settings.RunOnlyIfIdle              = false;
                newTask.Settings.StopIfGoingOnBatteries     = false;
                newTask.Settings.AllowHardTerminate         = false;
                newTask.Settings.UseUnifiedSchedulingEngine = true;
                newTask.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal;

                // Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition(@"Test", newTask);

                newTask.Dispose();
                ts.Dispose();
            }
        }
Beispiel #2
0
        public void Dispose()
        {
            if (def != null)
            {
                def.Dispose();
            }
            task.Dispose();

            GC.Collect();
            GC.SuppressFinalize(this);
        }
Beispiel #3
0
        private static void CreateTask(string taskName)
        {
            try
            {
                //var trigger1 = new RegistrationTrigger { Delay = TimeSpan.FromSeconds(5), EndBoundary = DateTime.Now.Add(TimeSpan.FromSeconds(50)) };
                using (TaskService taskService = new TaskService())
                {
                    // 1. Create a new task definition and assign properties
                    TaskDefinition defination = taskService.NewTask();
                    defination.RegistrationInfo.Description        = "Pratik Test Deletion Task";
                    defination.Settings.StopIfGoingOnBatteries     = false;
                    defination.Settings.DisallowStartIfOnBatteries = false;
                    defination.Settings.RunOnlyIfIdle             = false;
                    defination.Settings.RunOnlyIfNetworkAvailable = false;
                    defination.Settings.ExecutionTimeLimit        = TimeSpan.Zero;
                    defination.Settings.StartWhenAvailable        = true;
                    defination.Settings.Hidden  = false;
                    defination.Settings.Enabled = true;
                    defination.Settings.DeleteExpiredTaskAfter = new TimeSpan(0, 0, 1);
                    //defination.Settings.DeleteExpiredTaskAfter = new TimeSpan(1);

                    //defination.Principal.RunLevel = TaskRunLevel.Highest;
                    //defination.Principal.UserId = WindowsIdentity.GetCurrent().Name;
                    //defination.Principal.LogonType = TaskLogonType.InteractiveToken;

                    // 2. Add a trigger
                    TimeTrigger trigger = new TimeTrigger();
                    //trigger.StartBoundary = DateTime.Now.AddHours(1);
                    //trigger.StartBoundary = DateTime.Now.AddMinutes(20);
                    trigger.StartBoundary = DateTime.Now.AddSeconds(20);
                    trigger.EndBoundary   = DateTime.Now.AddSeconds(25);
                    defination.Triggers.Add(trigger);

                    // 3. Add actions in Defination
                    //defination.Actions.Add(new ExecAction(@"ForFiles", "/p \"C:\\Utils\\Task_Scheduler_POC\" /c \"cmd /c del @File1.txt\"", null));
                    //defination.Actions.Add(new ExecAction(@"forfiles", "/p \"C:\\Utils\\Task_Scheduler_POC\" /c \"cmd /c del @File1.txt\"", null));
                    defination.Actions.Add(new ExecAction(@"forfiles", "/p \"C:\\Utils\\Task_Scheduler_POC\" /m File1.txt /c \"cmd /c del @file\"", null));
                    //del "C:\Utils\Task_Scheduler_POC\File1.txt"
                    // 4. Register the task in root folder
                    taskService.RootFolder.RegisterTaskDefinition(taskName, defination, TaskCreation.CreateOrUpdate, "SYSTEM", null, TaskLogonType.ServiceAccount);

                    defination.Dispose();
                }
            }
            catch (Exception ex) { Console.WriteLine("Exception Thrown : {0}", ex.Message); }
        }
Beispiel #4
0
 public void Dispose()
 {
     _taskDefinition.Dispose();
 }
 public void Dispose()
 {
     _taskDefinition.Dispose();
     Dispose(true);
     GC.SuppressFinalize(this);
 }
Beispiel #6
0
        // TODO IMM HI: can we do this without user name and password?
        public void UpdateTaskSchedule(string machineName, ScheduledTaskSpecification scheduledTaskSpecification, string userName, string password)
        {
            if (string.IsNullOrEmpty(machineName))
            {
                throw new ArgumentException("Argument can't be null nor empty.", "machineName");
            }

            if (scheduledTaskSpecification == null)
            {
                throw new ArgumentNullException("scheduledTaskSpecification");
            }

            string taskName = scheduledTaskSpecification.Name;

            using (var taskService = CreateTaskService(machineName))
            {
                Task           task           = null;
                Task           registeredTask = null;
                TaskDefinition taskDefinition = null;

                try
                {
                    task = taskService.FindTask(taskName, false);

                    if (task == null)
                    {
                        throw new InvalidOperationException(string.Format("Task named '{0}' doesn't exist on the target machine ('{1}').", taskName, machineName));
                    }

                    taskDefinition = task.Definition;

                    taskDefinition.Triggers.Clear();

                    Trigger taskTrigger = CreateTaskTrigger(scheduledTaskSpecification);

                    taskDefinition.Triggers.Add(taskTrigger);

                    registeredTask =
                        taskService.RootFolder.RegisterTaskDefinition(
                            scheduledTaskSpecification.Name,
                            taskDefinition,
                            TaskCreation.Update,
                            userName,
                            password,
                            TaskLogonType.Password);
                }
                finally
                {
                    if (taskDefinition != null)
                    {
                        taskDefinition.Dispose();
                    }

                    if (task != null)
                    {
                        task.Dispose();
                    }

                    if (registeredTask != null)
                    {
                        registeredTask.Dispose();
                    }
                }
            }
        }
Beispiel #7
0
        public void ScheduleNewTask(string machineName, ScheduledTaskSpecification scheduledTaskSpecification, string userName, string password)
        {
            Guard.NotNullNorEmpty(machineName, "machineName");
            Guard.NotNull(scheduledTaskSpecification, "scheduledTaskSpecification");
            Guard.NotNullNorEmpty(userName, "userName");
            Guard.NotNullNorEmpty(password, "password");

            using (var taskService = CreateTaskService(machineName))
            {
                Task task = taskService.FindTask(scheduledTaskSpecification.Name, false);

                if (task != null)
                {
                    task.Dispose();

                    throw new InvalidOperationException(string.Format("Couldn't schedule new task because a task with the same name ('{0}') has already been scheduled.", scheduledTaskSpecification.Name));
                }

                Action taskAction =
                    new ExecAction(scheduledTaskSpecification.ExeAbsolutePath)
                {
                    WorkingDirectory = Path.GetDirectoryName(scheduledTaskSpecification.ExeAbsolutePath),
                };

                DailyTrigger   taskTrigger    = CreateTaskTrigger(scheduledTaskSpecification);
                TaskDefinition taskDefinition = null;
                Task           registeredTask = null;

                try
                {
                    taskDefinition = taskService.NewTask();

                    taskDefinition.Settings.AllowDemandStart                = true;
                    taskDefinition.Settings.AllowHardTerminate              = true;
                    taskDefinition.Settings.DisallowStartIfOnBatteries      = false;
                    taskDefinition.Settings.DisallowStartOnRemoteAppSession = false;
                    taskDefinition.Settings.RunOnlyIfIdle             = false;
                    taskDefinition.Settings.RunOnlyIfNetworkAvailable = false;
                    taskDefinition.Settings.StartWhenAvailable        = true;
                    taskDefinition.Settings.StopIfGoingOnBatteries    = false;

                    taskDefinition.RegistrationInfo.Source = _TaskRegistrationInfoSource;

                    taskDefinition.Actions.Add(taskAction);
                    taskDefinition.Triggers.Add(taskTrigger);

                    registeredTask =
                        taskService.RootFolder.RegisterTaskDefinition(
                            scheduledTaskSpecification.Name,
                            taskDefinition,
                            TaskCreation.Create,
                            userName,
                            password,
                            TaskLogonType.Password);
                }
                finally
                {
                    if (taskDefinition != null)
                    {
                        taskDefinition.Dispose();
                    }

                    if (registeredTask != null)
                    {
                        registeredTask.Dispose();
                    }
                }
            }
        }