Exemple #1
0
        public static async Task <Result <int> > Install()
        {
            var res1 = ScheduledTasks.RegisterUserScheduledTask(ScheduledTasks.ComplianceCheck.TaskName, ScheduledTasks.ComplianceCheck.ActionPath, ScheduledTasks.ComplianceCheck.ActionArguments, ScheduledTasks.ComplianceCheck.TaskDescription, new List <Trigger> {
                ScheduledTasks.UnlockTrigger(), ScheduledTasks.LoginTrigger(), ScheduledTasks.EventTrigger(ScheduledTasks.ComplianceCheck.EventId)
            })
                       .Try()
                       .Match(result => new Result <Unit>(Unit.Default), exception => new Result <Unit>(new Exception($"Failed to register task: {ScheduledTasks.ComplianceCheck.TaskName}", exception)));

            var res2 = ScheduledTasks.RegisterSystemScheduledTask(ScheduledTasks.ComplianceSystemMeasurements.TaskName, ScheduledTasks.ComplianceSystemMeasurements.ActionPath, ScheduledTasks.ComplianceSystemMeasurements.ActionArguments, ScheduledTasks.ComplianceSystemMeasurements.TaskDescription, new List <Trigger> {
                ScheduledTasks.HourlyTrigger(), ScheduledTasks.EventTrigger(ScheduledTasks.ComplianceSystemMeasurements.EventId)
            })
                       .Try()
                       .Match(result => new Result <Unit>(Unit.Default), exception => new Result <Unit>(new Exception($"Failed to register task: {ScheduledTasks.ComplianceSystemMeasurements.TaskName}", exception)));

            var res3 = ScheduledTasks.RegisterUserScheduledTask(ScheduledTasks.ComplianceUserMeasurements.TaskName, ScheduledTasks.ComplianceUserMeasurements.ActionPath, ScheduledTasks.ComplianceUserMeasurements.ActionArguments, ScheduledTasks.ComplianceUserMeasurements.TaskDescription, new List <Trigger> {
                ScheduledTasks.HourlyTrigger(), ScheduledTasks.LoginTrigger(), ScheduledTasks.EventTrigger(ScheduledTasks.ComplianceUserMeasurements.EventId)
            })
                       .Try()
                       .Match(result => new Result <Unit>(Unit.Default), exception => new Result <Unit>(new Exception($"Failed to register task: {ScheduledTasks.ComplianceUserMeasurements.TaskName}", exception)));

            //var res4 = ScheduledTasks.RegisterSystemManualTask(ScheduledTasks.FullSystemDiskCleanup.TaskName,
            //        ScheduledTasks.FullSystemDiskCleanup.ActionPath, ScheduledTasks.FullSystemDiskCleanup.ActionArguments,
            //    ScheduledTasks.FullSystemDiskCleanup.TaskDescription, new List<Trigger> { ScheduledTasks.HourlyTrigger(), ScheduledTasks.EventTrigger(ScheduledTasks.FullSystemDiskCleanup.EventId) })
            //    .Match(result => new Result<Unit>(Unit.Default), exception => new Result<Unit>(new Exception($"Failed to register task: {ScheduledTasks.FullSystemDiskCleanup.TaskName}", exception)));

            var res5 =
                (await WindowsEventLog.CreateEventSource().ConfigureAwait(false))
                .Match(unit => new Result <Unit>(Unit.Default), exception => new Result <Unit>(new Exception("Failed to register event log source.", exception)));

            var installResult = new List <Result <Unit> > {
                res1, res2, res3, res5
            }.ToResult().Match(exitCodes => new Result <int>(0), exception => new Result <int>(exception));

            return(await Task.FromResult(installResult).ConfigureAwait(false));
        }
Exemple #2
0
        public static async Task <Result <int> > UnInstall()
        {
            var res1 = ScheduledTasks.UnRegisterScheduledTask(ScheduledTasks.ComplianceCheck.TaskName).Match(result => new Result <int>(0), exception => new Result <int>(exception));
            var res2 = ScheduledTasks.UnRegisterScheduledTask(ScheduledTasks.ComplianceSystemMeasurements.TaskName).Match(result => new Result <int>(0), exception => new Result <int>(exception));
            var res3 = ScheduledTasks.UnRegisterScheduledTask(ScheduledTasks.ComplianceUserMeasurements.TaskName).Match(result => new Result <int>(0), exception => new Result <int>(exception));
            //var res4 = ScheduledTasks.UnRegisterScheduledTask(ScheduledTasks.FullSystemDiskCleanup.TaskName).Match(result => new Result<int>(0), exception => new Result<int>(exception));
            var unInstallResult = new List <Result <int> > {
                res1, res2, res3
            }.ToResult().Match(exitCodes => new Result <int>(exitCodes.Sum()), exception => new Result <int>(exception));

            return(await Task.FromResult(unInstallResult).ConfigureAwait(false));
        }
Exemple #3
0
 public static Try <Unit> RegisterUserScheduledTask(Some <string> taskName, Some <FileInfo> exeFile,
                                                    Some <string> arguments, Some <string> taskDescription, Some <List <Trigger> > triggers) => () =>
 {
     using (var ts = TaskService.Instance)
     {
         using (var td = ts.NewTask())
         {
             td.RegistrationInfo.Description = taskDescription.Value;
             td.Settings.MultipleInstances   = TaskInstancesPolicy.StopExisting;
             td.Actions.Add(new ExecAction($"\"{exeFile.Value.FullName}\"", arguments.Value, exeFile.Value.Directory.FullName));
             foreach (var trigger in triggers.Value)
             {
                 td.Triggers.Add(trigger);
             }
             td.Principal.GroupId  = ScheduledTasks.BuiltInUsers();
             td.Principal.RunLevel = TaskRunLevel.LUA;
             ts.RootFolder.RegisterTaskDefinition(taskName.Value, td);
         }
     }
     return(new Result <Unit>(Unit.Default));
 };
Exemple #4
0
        /// <summary>
        /// Load compliance measurement and check for non-compliance. If non-compliance and double check is true, trigger a new measurement to make sure it is still non-compliant.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="loadInfo"></param>
        /// <param name="isNonCompliant"></param>
        /// <param name="scheduledTask"></param>
        /// <param name="doubleCheck"></param>
        /// <returns></returns>
        public static async Task <T> LoadInfo <T>(Func <Task <T> > loadInfo, Func <T, bool> isNonCompliant, Some <ScheduledTaskInfo> scheduledTask, bool doubleCheck)
        {
            if (loadInfo == null)
            {
                throw new ArgumentNullException(nameof(loadInfo));
            }

            Func <bool> checkIfDoubleCheckShouldBeRun       = () => DoubleCheck.ShouldRunDoubleCheckAction(scheduledTask.Value.TaskName);
            Func <Task <Result <Unit> > > doubleCheckAction = async() =>
            {
                return(await DoubleCheck.RunDoubleCheck(scheduledTask.Value.TaskName, async() => await ScheduledTasks.RunScheduledTask(scheduledTask, true).ConfigureAwait(false), doubleCheck).ConfigureAwait(false));
            };

            return(await LoadInfoPure(loadInfo, isNonCompliant, checkIfDoubleCheckShouldBeRun, doubleCheckAction).ConfigureAwait(false));
        }