private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { try { switch (e.ClickedItem.Text) { case MENU_STRIP_NEW_SYNC: ScheduledTaskType = ScheduledTaskTypeEnum.Sync; break; case MENU_STRIP_NEW_CHECK: ScheduledTaskType = ScheduledTaskTypeEnum.Check; break; case MENU_STRIP_NEW_DIFF: ScheduledTaskType = ScheduledTaskTypeEnum.Diff; break; default: throw new ArgumentOutOfRangeException($@"Menu item '{e.ClickedItem.Text}' does not exist"); } CreateScheduledTask(ScheduledTaskType); DisplayTaskScheduleItems(); } catch (Exception ex) { Log.Fatal(ex); } }
private static string AddLoggingToArgsForSchedule(string args, ScheduledTaskTypeEnum scheduledTaskType) { string appDir = Path.GetDirectoryName(Properties.Settings.Default.ConfigFileLocation); string logDir = Properties.Settings.Default.LogFileDirectory; string logFilename = $"<DATETIME> {scheduledTaskType.ToString().ToLower()}.log"; // the powershell script will replace <DATETIME> string newArgs = $@"--log ""{appDir}\{logDir}\{logFilename}"" {args}"; return(newArgs); }
private void ItemClicked(ScheduledTaskTypeEnum scheduledTaskType) { try { CreateScheduledTask(scheduledTaskType); DisplayTaskScheduleItems(); } catch (Exception ex) { Log.Fatal(ex); } }
private static string AddLoggingToArgsForSchedule(string args, ScheduledTaskTypeEnum scheduledTaskType) { FileTarget fileTarget = (FileTarget)LogManager.Configuration.FindTargetByName("file"); // Need to set timestamp here if filename uses date. // For example - filename="${basedir}/logs/${shortdate}/trace.log" LogEventInfo logEventInfo = new LogEventInfo { TimeStamp = DateTime.Now }; string fileName = fileTarget.FileName.Render(logEventInfo); string logDir = Path.GetDirectoryName(fileName); string logFilename = $"<DATETIME> {scheduledTaskType.ToString().ToLower()}.log"; // the powershell script will replace <DATETIME> string newArgs = $@"--log ""{logDir}\{logFilename}"" {args}"; return(newArgs); }
private void CreateScheduledTask(ScheduledTaskTypeEnum scheduledTaskType) { string taskCommand; string taskDescription; switch (scheduledTaskType) { case ScheduledTaskTypeEnum.Sync: taskCommand = @"sync"; taskDescription = @"This task was created by Elucidate. " + @"| It performs the SnapRAID Sync command. " + $@"| SnapRAID config file: {Properties.Settings.Default.ConfigFileLocation}"; break; case ScheduledTaskTypeEnum.Check: taskCommand = @"check"; taskDescription = @"This task was created by Elucidate. " + @"| It performs the SnapRAID Check command. " + $@"| SnapRAID config file: {Properties.Settings.Default.ConfigFileLocation}"; break; case ScheduledTaskTypeEnum.Diff: taskCommand = @"diff"; taskDescription = @"This task was created by Elucidate. " + @"| It performs the SnapRAID Diff command. " + $@"| SnapRAID config file: {Properties.Settings.Default.ConfigFileLocation}"; break; default: return; } using (TaskService ts = new TaskService()) { // Create a new task definition and assign properties TaskDefinition td = ts.NewTask(); td.Data = "https://github.com/BlueBlock/Elucidate"; td.RegistrationInfo.Author = "Elucidate"; td.RegistrationInfo.Description = taskDescription; //td.RegistrationInfo.Documentation = "https://github.com/BlueBlock/Elucidate"; td.Settings.DisallowStartIfOnBatteries = true; td.Settings.Enabled = true; td.Settings.ExecutionTimeLimit = TimeSpan.FromDays(1); td.Settings.Hidden = true; td.Settings.Priority = ProcessPriorityClass.Normal; td.Settings.RunOnlyIfIdle = false; td.Settings.RunOnlyIfNetworkAvailable = false; td.Settings.StopIfGoingOnBatteries = true; bool newVer = (ts.HighestSupportedVersion >= new Version(1, 2)); if (newVer) { td.Triggers.Add(new DailyTrigger { DaysInterval = 1, StartBoundary = DateTime.Now + TimeSpan.FromMinutes(15) }); } string args = Util.FormatSnapRaidCommandArgs(taskCommand.ToLower(), out _); args = AddLoggingToArgsForSchedule(args, ScheduledTaskType); args = args.Replace(@"""", @"\"""); ExecAction tsAction = new ExecAction { Path = "powershell.exe", Arguments = $@"-File ""{Path.Combine(Environment.CurrentDirectory, appExecuteScript)}"" -exe ""{Properties.Settings.Default.SnapRAIDFileLocation}"" -args ""{args}"" ", WorkingDirectory = Path.GetDirectoryName(Properties.Settings.Default.SnapRAIDFileLocation) }; td.Actions.Add(tsAction); // Create an action which sends an email // td.Actions.Add(new EmailAction("Testing", "*****@*****.**", "*****@*****.**", "You've got mail.", "mail.myisp.com")); ts.RootFolder.RegisterTaskDefinition( path: $@"{TASK_FOLDER}\{GetNewTaskName()}", definition: td, createType: TaskCreation.CreateOrUpdate, userId: null, password: null, logonType: TaskLogonType.InteractiveToken); } }