コード例 #1
0
ファイル: Trigger.cs プロジェクト: BclEx/AdamsyncEx
 internal virtual void Bind(ITaskDefinition iTaskDef)
 {
     var o = iTaskDef.Triggers;
     _v2Trigger = o.Create(_ttype);
     Marshal.ReleaseComObject(o);
     foreach (string str in unboundValues.Keys)
     {
         try
         {
             var obj2 = unboundValues[str];
             CheckBindValue(str, ref obj2);
             _v2Trigger.GetType().InvokeMember(str, BindingFlags.SetProperty, null, _v2Trigger, new object[] { obj2 });
         }
         catch (TargetInvocationException ex) { throw ex.InnerException; }
         catch { }
     }
     unboundValues.Clear();
     unboundValues = null;
     _repititionPattern = new RepetitionPattern(this);
     _repititionPattern.Bind();
 }
コード例 #2
0
        private static void InitializeSchduledTask()
        {
            TimeSpan          howOften = new TimeSpan(0, 0, 1, 0);
            TimeSpan          howLong  = new TimeSpan(0, 0, 3, 0);
            RepetitionPattern rp       = new RepetitionPattern(howOften, howLong, stopAtDurationEnd: true);

            TimeTrigger tt = new TimeTrigger();

            tt.StartBoundary = DateTime.Now;
            tt.Repetition    = rp;

            ShowMessageAction msg = new ShowMessageAction("Jeg er en ost", "Hvad er du?");
            TaskDefinition    td  = TaskService.Instance.NewTask();

            td.RegistrationInfo.Author = "Holle TechNolle";
            td.Actions.Add(msg);
            td.Triggers.Add(tt);

            TaskService.Instance.RootFolder.RegisterTaskDefinition("MsgTask", td);

            tt.Enabled = true;
        }
コード例 #3
0
        private static DailyTrigger CreateTaskTrigger(ScheduledTaskSpecification scheduledTaskSpecification)
        {
            DateTime now = DateTime.Now;

            var dailyTrigger =
                new DailyTrigger
            {
                DaysInterval  = 1,
                StartBoundary =
                    new DateTime(
                        now.Year,
                        now.Month,
                        now.Day,
                        scheduledTaskSpecification.ScheduledHour,
                        scheduledTaskSpecification.ScheduledMinute,
                        0),
            };

            if (scheduledTaskSpecification.ExecutionTimeLimitInMinutes > 0)
            {
                dailyTrigger.ExecutionTimeLimit =
                    TimeSpan.FromMinutes(scheduledTaskSpecification.ExecutionTimeLimitInMinutes);
            }

            if (scheduledTaskSpecification.RepetitionSpecification.Enabled)
            {
                RepetitionSpecification repetitionSpecification = scheduledTaskSpecification.RepetitionSpecification;
                RepetitionPattern       repetitionPattern       = dailyTrigger.Repetition;

                repetitionPattern.Duration          = repetitionSpecification.Duration;
                repetitionPattern.Interval          = repetitionSpecification.Interval;
                repetitionPattern.StopAtDurationEnd = repetitionSpecification.StopAtDurationEnd;
            }

            return(dailyTrigger);
        }
コード例 #4
0
        } // end check persistence method

        // list all scheduled tasks available to backdoor
        public void listPersistence(string persistMethod, string command, string commandArg, string theKey, string theVal, string theName, string filePath, string status, string option)
        {
            // determine whether option was specified
            bool optionSpecified = false;

            if (!option.Equals(""))
            {
                optionSpecified = true;
            }

            bool nameSpecified = false;
            bool schTaskExists = false;

            // if user specified they only want to list a specific schtask
            if (!theName.Equals(""))
            {
                nameSpecified = true;
                schTaskExists = lib.Utils.ScheduledTaskExists(theName);

                // if schtask exists, then look for that schtask
                if (schTaskExists)
                {
                    Console.WriteLine("");
                    Console.WriteLine("[*] INFO: Listing scheduled task details of name that was specified.");

                    TaskService        theTask       = new TaskService();
                    IEnumerable <Task> allOfTheTasks = theTask.AllTasks;
                    Console.WriteLine("");
                    Console.WriteLine("");
                    Console.WriteLine("");

                    foreach (Task task in allOfTheTasks)
                    {
                        string   schtaskName = task.Name;
                        DateTime runTime     = task.NextRunTime;
                        string   theRunTime  = runTime.ToString("G", CultureInfo.CurrentCulture);

                        // once we find the schtask, display its details
                        if (schtaskName.ToLower().Equals(theName.ToLower()))
                        {
                            ActionCollection allActions = task.Definition.Actions;

                            // getschtask owner
                            string             schtaskAction = allActions.Context;
                            SecurityIdentifier schtaskOwner  = task.SecurityDescriptor.Owner;
                            NTAccount          ntAccount     = (NTAccount)schtaskOwner.Translate(typeof(NTAccount));
                            string             owner         = ntAccount.ToString();
                            string             schtaskFolder = task.Folder.Path;


                            Console.WriteLine("[*] INFO: TASK NAME:");
                            Console.WriteLine(schtaskName);
                            Console.WriteLine("");
                            Console.WriteLine("[*] INFO: TASK PATH:");
                            Console.WriteLine(schtaskFolder);
                            Console.WriteLine("");
                            Console.WriteLine("[*] INFO: TASK OWNER:");
                            Console.WriteLine(owner);
                            Console.WriteLine("");
                            Console.WriteLine("[*] INFO: NEXT RUN TIME:");
                            Console.WriteLine(theRunTime);
                            Console.WriteLine("");

                            // get the frequency in which the schtask executes
                            TriggerCollection triggers    = task.Definition.Triggers;
                            string            triggerType = "";
                            foreach (Trigger trigger in triggers)
                            {
                                RepetitionPattern pattern = trigger.Repetition;

                                triggerType = trigger.TriggerType.ToString();
                                Console.WriteLine("[*] INFO: TASK TRIGGER:");
                                Console.WriteLine(triggerType);
                                Console.WriteLine("");
                            }


                            // get all actions and print
                            foreach (Microsoft.Win32.TaskScheduler.Action action in allActions)
                            {
                                Console.WriteLine("[*] INFO: TASK ACTION:");
                                Console.WriteLine(action.ToString());
                                Console.WriteLine("");
                            }
                        } // end once we find the schtask
                    }     // end for each task

                    return;
                } // end if schtask exists

                // if schtask doesn't exist
                else
                {
                    Console.WriteLine("");
                    Console.WriteLine("[-] ERROR: That scheduled task name does not exist. Please double check the name you provided.");
                    return;
                }
            } // end if user specified they only want to list a specific schtask


            // if user wants to see all schtasks
            else
            {
                Console.WriteLine("");
                Console.WriteLine("[*] INFO: Listing all scheduled tasks available to backdoor.");


                TaskService        ts       = new TaskService();
                IEnumerable <Task> allTasks = ts.AllTasks;
                bool schtaskExists          = lib.Utils.ScheduledTaskExists(theName);
                Console.WriteLine("");
                Console.WriteLine("");
                Console.WriteLine("");


                foreach (Task task in allTasks)
                {
                    string   schtaskName = task.Name;
                    DateTime runTime     = task.NextRunTime;
                    string   theRunTime  = runTime.ToString("G", CultureInfo.CurrentCulture);
                    bool     taskActive  = task.IsActive;


                    // only proceed to list schtask info if it is active
                    if (taskActive)
                    {
                        // get collection of all actions the schtask performs
                        ActionCollection allActions = task.Definition.Actions;

                        // getschtask owner
                        string             schtaskAction = allActions.Context;
                        SecurityIdentifier schtaskOwner  = task.SecurityDescriptor.Owner;
                        NTAccount          ntAccount     = (NTAccount)schtaskOwner.Translate(typeof(NTAccount));
                        string             owner         = ntAccount.ToString();
                        string             schtaskFolder = task.Folder.Path;

                        TriggerCollection triggers    = task.Definition.Triggers;
                        string            triggerType = "";
                        foreach (Trigger trigger in triggers)
                        {
                            RepetitionPattern pattern = trigger.Repetition;
                            triggerType = trigger.TriggerType.ToString();
                        }

                        // if option was specified, only display schtasks with frequency given
                        if (optionSpecified)
                        {
                            if (option.ToLower().Equals("hourly") && triggerType.ToLower().Equals("time"))
                            {
                                Console.WriteLine("[*] INFO: TASK NAME:");
                                Console.WriteLine(schtaskName);
                                Console.WriteLine("");
                                Console.WriteLine("[*] INFO: TASK PATH:");
                                Console.WriteLine(schtaskFolder);
                                Console.WriteLine("");
                                Console.WriteLine("[*] INFO: TASK OWNER:");
                                Console.WriteLine(owner);
                                Console.WriteLine("");
                                Console.WriteLine("[*] INFO: NEXT RUN TIME:");
                                Console.WriteLine(theRunTime);
                                Console.WriteLine("");

                                // get the frequency in which the schtask executes
                                TriggerCollection theTriggers    = task.Definition.Triggers;
                                string            theTriggerType = "";
                                foreach (Trigger trigger in theTriggers)
                                {
                                    RepetitionPattern pattern = trigger.Repetition;

                                    theTriggerType = trigger.TriggerType.ToString();
                                    Console.WriteLine("[*] INFO: TASK TRIGGER:");
                                    Console.WriteLine(theTriggerType);
                                    Console.WriteLine("");
                                }



                                // get all actions and print
                                foreach (Microsoft.Win32.TaskScheduler.Action action in allActions)
                                {
                                    Console.WriteLine("[*] INFO: TASK ACTION:");
                                    Console.WriteLine(action.ToString());
                                    Console.WriteLine("");
                                }

                                Console.WriteLine("");
                                Console.WriteLine("");
                                Console.WriteLine("");
                            }


                            else if (option.ToLower().Equals("daily") && triggerType.ToLower().Equals("daily"))
                            {
                                Console.WriteLine("[*] INFO: TASK NAME:");
                                Console.WriteLine(schtaskName);
                                Console.WriteLine("");
                                Console.WriteLine("[*] INFO: TASK PATH:");
                                Console.WriteLine(schtaskFolder);
                                Console.WriteLine("");
                                Console.WriteLine("[*] INFO: TASK OWNER:");
                                Console.WriteLine(owner);
                                Console.WriteLine("");
                                Console.WriteLine("[*] INFO: NEXT RUN TIME:");
                                Console.WriteLine(theRunTime);
                                Console.WriteLine("");

                                // get the frequency in which the schtask executes
                                TriggerCollection theTriggers    = task.Definition.Triggers;
                                string            theTriggerType = "";
                                foreach (Trigger trigger in theTriggers)
                                {
                                    RepetitionPattern pattern = trigger.Repetition;

                                    theTriggerType = trigger.TriggerType.ToString();
                                    Console.WriteLine("[*] INFO: TASK TRIGGER:");
                                    Console.WriteLine(theTriggerType);
                                    Console.WriteLine("");
                                }



                                // get all actions and print
                                foreach (Microsoft.Win32.TaskScheduler.Action action in allActions)
                                {
                                    Console.WriteLine("[*] INFO: TASK ACTION:");
                                    Console.WriteLine(action.ToString());
                                    Console.WriteLine("");
                                }

                                Console.WriteLine("");
                                Console.WriteLine("");
                                Console.WriteLine("");
                            }


                            else if ((option.ToLower().Equals("logon") && triggerType.ToLower().Equals("logon")) || (option.ToLower().Equals("boot") && triggerType.ToLower().Equals("boot")))
                            {
                                Console.WriteLine("[*] INFO: TASK NAME:");
                                Console.WriteLine(schtaskName);
                                Console.WriteLine("");
                                Console.WriteLine("[*] INFO: TASK PATH:");
                                Console.WriteLine(schtaskFolder);
                                Console.WriteLine("");
                                Console.WriteLine("[*] INFO: TASK OWNER:");
                                Console.WriteLine(owner);
                                Console.WriteLine("");
                                Console.WriteLine("[*] INFO: NEXT RUN TIME:");
                                Console.WriteLine(theRunTime);
                                Console.WriteLine("");

                                // get the frequency in which the schtask executes
                                TriggerCollection theTriggers    = task.Definition.Triggers;
                                string            theTriggerType = "";
                                foreach (Trigger trigger in theTriggers)
                                {
                                    RepetitionPattern pattern = trigger.Repetition;

                                    theTriggerType = trigger.TriggerType.ToString();
                                    Console.WriteLine("[*] INFO: TASK TRIGGER:");
                                    Console.WriteLine(theTriggerType);
                                    Console.WriteLine("");
                                }



                                // get all actions and print
                                foreach (Microsoft.Win32.TaskScheduler.Action action in allActions)
                                {
                                    Console.WriteLine("[*] INFO: TASK ACTION:");
                                    Console.WriteLine(action.ToString());
                                    Console.WriteLine("");
                                }

                                Console.WriteLine("");
                                Console.WriteLine("");
                                Console.WriteLine("");
                            }
                        } // end if option specified

                        // otherwise display as normal
                        else
                        {
                            Console.WriteLine("[*] INFO: TASK NAME:");
                            Console.WriteLine(schtaskName);
                            Console.WriteLine("");
                            Console.WriteLine("[*] INFO: TASK PATH:");
                            Console.WriteLine(schtaskFolder);
                            Console.WriteLine("");
                            Console.WriteLine("[*] INFO: TASK OWNER:");
                            Console.WriteLine(owner);
                            Console.WriteLine("");
                            Console.WriteLine("[*] INFO: NEXT RUN TIME:");
                            Console.WriteLine(theRunTime);
                            Console.WriteLine("");

                            // get the frequency in which the schtask executes
                            TriggerCollection theTriggers    = task.Definition.Triggers;
                            string            theTriggerType = "";
                            foreach (Trigger trigger in theTriggers)
                            {
                                RepetitionPattern pattern = trigger.Repetition;

                                theTriggerType = trigger.TriggerType.ToString();
                                Console.WriteLine("[*] INFO: TASK TRIGGER:");
                                Console.WriteLine(theTriggerType);
                                Console.WriteLine("");
                            }



                            // get all actions and print
                            foreach (Microsoft.Win32.TaskScheduler.Action action in allActions)
                            {
                                Console.WriteLine("[*] INFO: TASK ACTION:");
                                Console.WriteLine(action.ToString());
                                Console.WriteLine("");
                            }

                            Console.WriteLine("");
                            Console.WriteLine("");
                            Console.WriteLine("");
                        }
                    } // end if schtask is active
                }     // end iterating through each schtask
            }         // end if user wants to see all schtasks
        }             // end list persistence method
コード例 #5
0
 public IRepetitionPattern CreateRepetitionPattern(RepetitionPattern repetitionPattern)
 {
     return(new Dev2RepetitionPattern(repetitionPattern));
 }
コード例 #6
0
 public Dev2RepetitionPattern(RepetitionPattern nativeInstance)
 {
     _nativeInstance = nativeInstance;
 }
コード例 #7
0
        static void Main(string[] args)
        {
            try
            {
                OpenConfigFile();

                Console.WriteLine("Select one of the following options: ");
                Console.WriteLine("1. Create Handy Sync Task in Task Scheduler. ");
                Console.WriteLine("2. Remove Handy Sync Task from Task Scheduler. ");
                Console.WriteLine();
                ConsoleKeyInfo info = Console.ReadKey();
                if (info.Key == ConsoleKey.D1)
                {
                    Console.WriteLine();
                    Console.WriteLine("Creating HandySync Task.");
                    // Get the service on the local machine
                    using (TaskService ts = new TaskService())
                    {
                        try
                        {
                            // Create a new task definition and assign properties
                            TaskDefinition    td      = ts.NewTask();
                            int               minutes = Int32.Parse(config.AppSettings.Settings["SyncPeriodMinutes"].Value);
                            RepetitionPattern rp      = new RepetitionPattern(new TimeSpan(0, minutes, 0), TimeSpan.Zero);
                            td.RegistrationInfo.Description = "Runs the HandySync";

                            // Create a trigger that will fire the task at this time every other day
                            td.Triggers.Add(new DailyTrigger {
                                DaysInterval = 1, Repetition = rp, Enabled = true
                            });

                            // Create an action that will launch Notepad whenever the trigger fires
                            string HandySyncPath = config.AppSettings.Settings["HandySyncPath"].Value;
                            td.Actions.Add(new ExecAction(HandySyncPath + @"\HandySyncService.exe"));

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

                            // Remove the task we just created
                            //ts.RootFolder.DeleteTask("Test");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error: " + ex.Message);
                        }
                    }
                    Console.WriteLine("HandySync Task created successfully.");
                }
                else if (info.Key == ConsoleKey.D2)
                {
                    using (TaskService ts = new TaskService())
                    {
                        try
                        {
                            Console.WriteLine();
                            ts.RootFolder.DeleteTask("HandySync");
                            Console.WriteLine("Handy Sync Task removed successfully.");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error: " + ex.Message);
                        }
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Invalid Option.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            Console.WriteLine();
            Console.WriteLine("PRESS ANY KEY TO EXIT");
            Console.ReadKey();
        }