/// <summary> /// Installs the windows scheduled tasks. /// </summary> public void InstallScheduledTasks() { using (TaskService taskService = new TaskService()) { TaskFolder taskFolder = taskService.RootFolder.SubFolders.FirstOrDefault(f => f.Name == FOLDER_NAME) ?? taskService.RootFolder.CreateFolder(FOLDER_NAME); string taskXml = AssemblyResourceReader.ReadAsString(TEMPLATE_FILE); Dictionary<string, string> namespaces = new Dictionary<string, string> { { Constants.Task_XmlNamespacePrefix, Constants.Task_XmlNamespaceUri } }; List<XmlReplacement> replacements = new List<XmlReplacement>(); string startBoundaryVal = DateTime.Now.ToString("s"); replacements.Create(Constants.XPath_StartBoundary, null, currentValue => startBoundaryVal); XmlReplacement commandReplacement = replacements.Create(Constants.XPath_Command, null, null); string json = AssemblyResourceReader.ReadAsString(TASK_JSON_FILE) .Replace("{websiteName}", HttpUtility.JavaScriptStringEncode(this.installOptions.WebsiteName)); XmlUpdater updater = new XmlUpdater(); JArray tasks = JArray.Parse(json); foreach (JToken task in tasks) { string taskName = task.Value<string>(TASK_NAME_JSON_KEY); this.installLogger.Log(string.Format(Messages.TASK_CreateScheduledTask, taskName)); XmlDocument doc = new XmlDocument(); doc.LoadXml(taskXml); string executablePath = Path.Combine(this.installVariables.BasePath, task.Value<string>(COMMAND_PATH_JSON_KEY)); commandReplacement.ReplacementValue = currentValue => executablePath; updater.Update(doc, namespaces, replacements); doc.Save(this.tempFile); TaskDefinition taskDefinition = taskService.NewTaskFromFile(this.tempFile); float interval = task.Value<float>(INTERVAL_MINUTES_JSON_KEY); this.AddSchedule(taskDefinition, interval); taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, @"NT AUTHORITY\SYSTEM", null, TaskLogonType.ServiceAccount); this.AddFolderPermission(Directory.GetParent(executablePath).FullName); this.installLogger.LogSuccess(Messages.MAIN_StepComplete); } File.Delete(this.tempFile); } }