Exemple #1
0
        public bool createTask(bool wakeToRun, string startTime, TaskActions[] tActions)
        {
            //string xmlFilename = "epg123Task.xml";
            DateTime date = DateTime.Parse(string.Format("{0}/{1}/{2}T{3}:{4}",
                                                         DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                                                         startTime.Substring(0, 2), startTime.Substring(3, 2)));

            // create registration
            registrationInfoType registration = new registrationInfoType()
            {
                Author      = "GaRyan2's epg123",
                Description = "Utility to update the Windows Media Center Electronic Program Guide by downloading guide information from Schedules Direct and importing the created .mxf file.",
                URI         = "\\epg123_update"
            };

            // create trigger
            triggersType triggers = new triggersType()
            {
                Items = new triggerBaseType[]
                {
                    new calendarTriggerType()
                    {
                        id   = "epg123 Daily Trigger",
                        Item = new dailyScheduleType()
                        {
                            DaysInterval          = 1,
                            DaysIntervalSpecified = true
                        },
                        StartBoundary          = date,
                        StartBoundarySpecified = true
                    }
                }
            };

            // create settings
            settingsType settings = new settingsType()
            {
                DisallowStartIfOnBatteries = false,
                StopIfGoingOnBatteries     = false,
                ExecutionTimeLimit         = "PT23H",
                Priority         = 6,
                RestartOnFailure = new restartType()
                {
                    Count    = 5,
                    Interval = "PT30M"
                },
                StartWhenAvailable = true,
                //RunOnlyIfNetworkAvailable = true,
                WakeToRun = wakeToRun
            };

            // create principal
            // Windows10 1607 Anniversary Update introduced a bug which only executes the first
            // task action unless running a service or possibly administrator?
            principalsType principals;

            if (isWindows10())
            {
                principals = new principalsType()
                {
                    Principal = new principalType()
                    {
                        id                = @"NT AUTHORITY\NETWORKSERVICE",
                        UserId            = "S-1-5-20",
                        RunLevel          = runLevelType.HighestAvailable,
                        RunLevelSpecified = true
                    }
                };
            }
            else
            {
                principals = new principalsType()
                {
                    Principal = new principalType()
                    {
                        id                 = @"Author",
                        LogonType          = logonType.S4U,
                        LogonTypeSpecified = true,
                        RunLevel           = runLevelType.HighestAvailable,
                        RunLevelSpecified  = true
                    }
                };
            }

            // create action(s)
            execType[] executions = new execType[tActions.Length];
            for (int i = 0; i < tActions.Length; ++i)
            {
                executions[i] = new execType()
                {
                    id               = string.Format("epg123 Execution Action {0}", i + 1),
                    Command          = tActions[i].Path,
                    Arguments        = tActions[i].Arguments,
                    WorkingDirectory = Helper.ExecutablePath
                };
            }
            actionsType actions = new actionsType()
            {
                Exec = executions
            };

            // build complete task
            taskType newTask = new taskType()
            {
                RegistrationInfo = registration,
                Triggers         = triggers,
                Settings         = settings,
                Principals       = principals,
                Actions          = actions,
            };

            // serialize to xml and save to the drive
            try
            {
                using (StreamWriter stream = new StreamWriter(Helper.Epg123TaskXmlPath, false, Encoding.Unicode))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(taskType));
                    TextWriter    writer     = stream;
                    serializer.Serialize(writer, newTask);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteError(string.Format("Failed to create a {0} daily update task XML file. message: {1}", startTime, ex.Message));
            }

            return(false);
        }
Exemple #2
0
        public bool CreateTask(bool wakeToRun, string startTime, TaskActions[] tActions)
        {
            //string xmlFilename = "epg123Task.xml";
            var date = DateTime.Parse($"{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}T{startTime.Substring(0, 2)}:{startTime.Substring(3, 2)}");

            // create registration
            var registration = new registrationInfoType()
            {
                Author             = "GaRyan2's epg123",
                Description        = "Utility to update the Windows Media Center Electronic Program Guide by downloading guide information from Schedules Direct and importing the created .mxf file.",
                URI                = "\\epg123_update",
                SecurityDescriptor = "D:(A;;FRFWSDWDWO;;;BA)(A;;FRFWSDWDWO;;;SY)(A;;FRFWFXDTDCSDWD;;;NS)(A;;FXFR;;;AU)"
            };

            // create trigger
            var triggers = new triggersType()
            {
                Items = new triggerBaseType[]
                {
                    new calendarTriggerType()
                    {
                        id   = "epg123 Daily Trigger",
                        Item = new dailyScheduleType()
                        {
                            DaysInterval          = 1,
                            DaysIntervalSpecified = true
                        },
                        StartBoundary          = date,
                        StartBoundarySpecified = true
                    }
                }
            };

            // create settings
            var settings = new settingsType()
            {
                DisallowStartIfOnBatteries = false,
                StopIfGoingOnBatteries     = false,
                ExecutionTimeLimit         = "PT23H",
                Priority         = 6,
                RestartOnFailure = new restartType()
                {
                    Count    = 5,
                    Interval = "PT30M"
                },
                StartWhenAvailable = true,
                //RunOnlyIfNetworkAvailable = true,
                WakeToRun = wakeToRun
            };

            // create principal
            // Windows10 1607 Anniversary Update introduced a bug which only executes the first
            // task action unless running a service or possibly administrator?
            // Also, network service does not have access to some registry keys in Win7
            var principals = new principalsType()
            {
                Principal = new principalType()
                {
                    id                = @"SYSTEM",
                    UserId            = "S-1-5-18",
                    RunLevel          = runLevelType.HighestAvailable,
                    RunLevelSpecified = true
                }
            };

            // create action(s)
            var executions = new execType[tActions.Length];

            for (var i = 0; i < tActions.Length; ++i)
            {
                executions[i] = new execType()
                {
                    id               = $"epg123 Execution Action {i + 1}",
                    Command          = tActions[i].Path,
                    Arguments        = tActions[i].Arguments.Replace("http:", "EPG:"),
                    WorkingDirectory = Helper.ExecutablePath
                };
            }

            var actions = new actionsType()
            {
                Exec = executions
            };

            // build complete task
            var newTask = new taskType()
            {
                RegistrationInfo = registration,
                Triggers         = triggers,
                Settings         = settings,
                Principals       = principals,
                Actions          = actions,
            };

            // serialize to xml and save to the drive
            try
            {
                using (var stream = new StreamWriter(Helper.Epg123TaskXmlPath, false, Encoding.Unicode))
                {
                    var        serializer = new XmlSerializer(typeof(taskType));
                    TextWriter writer     = stream;
                    serializer.Serialize(writer, newTask);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteError($"Failed to create a {startTime} daily update task XML file. message: {ex.Message}");
            }
            return(false);
        }