Exemplo n.º 1
0
 public Collection <ScheduledJobTrigger> GetJobTriggers(string taskId)
 {
     if (!string.IsNullOrEmpty(taskId))
     {
         ITaskDefinition variable = this.FindTask(taskId);
         Collection <ScheduledJobTrigger> scheduledJobTriggers = new Collection <ScheduledJobTrigger>();
         ITriggerCollection triggers = variable.Triggers;
         if (triggers != null)
         {
             foreach (ITrigger trigger in triggers)
             {
                 ScheduledJobTrigger scheduledJobTrigger = this.CreateJobTrigger(trigger);
                 if (scheduledJobTrigger != null)
                 {
                     scheduledJobTriggers.Add(scheduledJobTrigger);
                 }
                 else
                 {
                     object[] id = new object[2];
                     id[0] = taskId;
                     id[1] = trigger.Id;
                     string str = StringUtil.Format(ScheduledJobErrorStrings.UnknownTriggerType, id);
                     throw new ScheduledJobException(str);
                 }
             }
         }
         return(scheduledJobTriggers);
     }
     else
     {
         throw new PSArgumentException("taskId");
     }
 }
Exemplo n.º 2
0
        private CimInstance ConvertToOnce(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass    cimClass    = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskTimeTrigger");
            CimInstance cimInstance = new CimInstance(cimClass);

            cimInstance.CimInstanceProperties["RandomDelay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);

            if (trigger.RepetitionInterval != null && trigger.RepetitionDuration != null)
            {
                CimClass    cimRepClass    = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskRepetitionPattern");
                CimInstance cimRepInstance = new CimInstance(cimRepClass);

                cimRepInstance.CimInstanceProperties["Interval"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RepetitionInterval.Value);

                if (trigger.RepetitionDuration == TimeSpan.MaxValue)
                {
                    cimRepInstance.CimInstanceProperties["StopAtDurationEnd"].Value = false;
                }
                else
                {
                    cimRepInstance.CimInstanceProperties["StopAtDurationEnd"].Value = true;
                    cimRepInstance.CimInstanceProperties["Duration"].Value          = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RepetitionDuration.Value);
                }

                cimInstance.CimInstanceProperties["Repetition"].Value = cimRepInstance;
            }

            AddCommonProperties(trigger, cimInstance);
            return(cimInstance);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Retrieves job triggers from WTS with provided task Id.
        /// </summary>
        /// <param name="taskId">Task Id</param>
        /// <exception cref="ScheduledJobException">Task not found.</exception>
        /// <returns>ScheduledJobTriggers</returns>
        public Collection <ScheduledJobTrigger> GetJobTriggers(
            string taskId)
        {
            if (string.IsNullOrEmpty(taskId))
            {
                throw new PSArgumentException("taskId");
            }

            ITaskDefinition iTaskDefinition = FindTask(taskId);

            Collection <ScheduledJobTrigger> jobTriggers = new Collection <ScheduledJobTrigger>();
            ITriggerCollection iTriggerCollection        = iTaskDefinition.Triggers;

            if (iTriggerCollection != null)
            {
                foreach (ITrigger iTrigger in iTriggerCollection)
                {
                    ScheduledJobTrigger jobTrigger = CreateJobTrigger(iTrigger);
                    if (jobTrigger == null)
                    {
                        string msg = StringUtil.Format(ScheduledJobErrorStrings.UnknownTriggerType, taskId, iTrigger.Id);
                        throw new ScheduledJobException(msg);
                    }

                    jobTriggers.Add(jobTrigger);
                }
            }

            return(jobTriggers);
        }
Exemplo n.º 4
0
 private bool UpdateTrigger(ScheduledJobTrigger trigger, TriggerFrequency triggerFrequency)
 {
     if (triggerFrequency != TriggerFrequency.None)
     {
         //
         // User has specified a specific trigger type.
         // Parameters have been validated for this trigger type.
         //
         if (triggerFrequency != trigger.Frequency)
         {
             // Changing to a new trigger type.
             return(CreateTrigger(trigger, triggerFrequency));
         }
         else
         {
             // Modifying existing trigger type.
             return(ModifyTrigger(trigger, triggerFrequency));
         }
     }
     else
     {
         // We are updating an existing trigger.  Need to validate params
         // against each trigger type we are updating.
         return(ModifyTrigger(trigger, trigger.Frequency, true));
     }
 }
Exemplo n.º 5
0
 private void ModifyStartupTrigger(ScheduledJobTrigger trigger)
 {
     if (MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay)))
     {
         trigger.RandomDelay = _randomDelay;
     }
 }
Exemplo n.º 6
0
 private void ModifyStartupTrigger(ScheduledJobTrigger trigger)
 {
     if (base.MyInvocation.BoundParameters.ContainsKey(this._paramRandomDelay))
     {
         trigger.RandomDelay = this._randomDelay;
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Creates a ScheduledJobTrigger object based on a provided WTS ITrigger.
        /// </summary>
        /// <param name="iTrigger">ITrigger</param>
        /// <returns>ScheduledJobTrigger</returns>
        private ScheduledJobTrigger CreateJobTrigger(
            ITrigger iTrigger)
        {
            ScheduledJobTrigger rtnJobTrigger = null;

            if (iTrigger is IBootTrigger)
            {
                IBootTrigger iBootTrigger = (IBootTrigger)iTrigger;
                rtnJobTrigger = ScheduledJobTrigger.CreateAtStartupTrigger(
                    ParseWTSTime(iBootTrigger.Delay),
                    ConvertStringId(iBootTrigger.Id),
                    iBootTrigger.Enabled);
            }
            else if (iTrigger is ILogonTrigger)
            {
                ILogonTrigger iLogonTrigger = (ILogonTrigger)iTrigger;
                rtnJobTrigger = ScheduledJobTrigger.CreateAtLogOnTrigger(
                    iLogonTrigger.UserId,
                    ParseWTSTime(iLogonTrigger.Delay),
                    ConvertStringId(iLogonTrigger.Id),
                    iLogonTrigger.Enabled);
            }
            else if (iTrigger is ITimeTrigger)
            {
                ITimeTrigger iTimeTrigger = (ITimeTrigger)iTrigger;
                TimeSpan     repInterval  = ParseWTSTime(iTimeTrigger.Repetition.Interval);
                TimeSpan     repDuration  = (repInterval != TimeSpan.Zero && iTimeTrigger.Repetition.StopAtDurationEnd == false) ?
                                            TimeSpan.MaxValue : ParseWTSTime(iTimeTrigger.Repetition.Duration);
                rtnJobTrigger = ScheduledJobTrigger.CreateOnceTrigger(
                    DateTime.Parse(iTimeTrigger.StartBoundary, CultureInfo.InvariantCulture),
                    ParseWTSTime(iTimeTrigger.RandomDelay),
                    repInterval,
                    repDuration,
                    ConvertStringId(iTimeTrigger.Id),
                    iTimeTrigger.Enabled);
            }
            else if (iTrigger is IDailyTrigger)
            {
                IDailyTrigger iDailyTrigger = (IDailyTrigger)iTrigger;
                rtnJobTrigger = ScheduledJobTrigger.CreateDailyTrigger(
                    DateTime.Parse(iDailyTrigger.StartBoundary, CultureInfo.InvariantCulture),
                    (Int32)iDailyTrigger.DaysInterval,
                    ParseWTSTime(iDailyTrigger.RandomDelay),
                    ConvertStringId(iDailyTrigger.Id),
                    iDailyTrigger.Enabled);
            }
            else if (iTrigger is IWeeklyTrigger)
            {
                IWeeklyTrigger iWeeklyTrigger = (IWeeklyTrigger)iTrigger;
                rtnJobTrigger = ScheduledJobTrigger.CreateWeeklyTrigger(
                    DateTime.Parse(iWeeklyTrigger.StartBoundary, CultureInfo.InvariantCulture),
                    (Int32)iWeeklyTrigger.WeeksInterval,
                    ConvertMaskToDaysOfWeekArray(iWeeklyTrigger.DaysOfWeek),
                    ParseWTSTime(iWeeklyTrigger.RandomDelay),
                    ConvertStringId(iWeeklyTrigger.Id),
                    iWeeklyTrigger.Enabled);
            }

            return(rtnJobTrigger);
        }
Exemplo n.º 8
0
        private bool CreateTrigger(ScheduledJobTrigger trigger, TriggerFrequency triggerFrequency)
        {
            TriggerFrequency triggerFrequency1 = triggerFrequency;

            switch (triggerFrequency1)
            {
            case TriggerFrequency.Once:
                {
                    if (trigger.Frequency == triggerFrequency || this.ValidateOnceParams(trigger))
                    {
                        this.CreateOnceTrigger(trigger);
                        break;
                    }
                    else
                    {
                        return(false);
                    }
                }

            case TriggerFrequency.Daily:
                {
                    if (trigger.Frequency == triggerFrequency || this.ValidateDailyParams(trigger))
                    {
                        this.CreateDailyTrigger(trigger);
                        break;
                    }
                    else
                    {
                        return(false);
                    }
                }

            case TriggerFrequency.Weekly:
                {
                    if (trigger.Frequency == triggerFrequency || this.ValidateWeeklyParams(trigger))
                    {
                        this.CreateWeeklyTrigger(trigger);
                        break;
                    }
                    else
                    {
                        return(false);
                    }
                }

            case TriggerFrequency.AtLogon:
                {
                    this.CreateAtLogonTrigger(trigger);
                    break;
                }

            case TriggerFrequency.AtStartup:
            {
                this.CreateAtStartupTrigger(trigger);
                break;
            }
            }
            return(true);
        }
Exemplo n.º 9
0
        private bool ValidateOnceParams(ScheduledJobTrigger trigger = null)
        {
            if (MyInvocation.BoundParameters.ContainsKey(_paramDaysInterval))
            {
                string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysInterval, ScheduledJobErrorStrings.TriggerOnceType);
                WriteValidationError(msg);
                return(false);
            }
            if (MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval))
            {
                string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidWeeksInterval, ScheduledJobErrorStrings.TriggerOnceType);
                WriteValidationError(msg);
                return(false);
            }
            if (MyInvocation.BoundParameters.ContainsKey(_paramUser))
            {
                string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidUser, ScheduledJobErrorStrings.TriggerOnceType);
                WriteValidationError(msg);
                return(false);
            }
            if (MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek))
            {
                string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysOfWeek, ScheduledJobErrorStrings.TriggerOnceType);
                WriteValidationError(msg);
                return(false);
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInfiniteDuration))
            {
                _repDuration = TimeSpan.MaxValue;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval) || MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration) ||
                MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInfiniteDuration))
            {
                // Validate Once trigger repetition parameters.
                try
                {
                    ScheduledJobTrigger.ValidateOnceRepetitionParams(_repInterval, _repDuration);
                }
                catch (PSArgumentException e)
                {
                    WriteValidationError(e.Message);
                    return(false);
                }
            }

            if (trigger != null)
            {
                if (trigger.At == null && !MyInvocation.BoundParameters.ContainsKey(_paramAt))
                {
                    string msg = StringUtil.Format(ScheduledJobErrorStrings.MissingAtTime, ScheduledJobErrorStrings.TriggerOnceType);
                    WriteValidationError(msg);
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 10
0
        private CimInstance ConvertToDefault(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass    cimClass = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskTrigger");
            CimInstance result   = new CimInstance(cimClass);

            AddCommonProperties(trigger, result);
            return(result);
        }
        private CimInstance ConvertToDefault(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass    @class      = cimSession.GetClass(JobTriggerToCimInstanceConverter.CIM_TRIGGER_NAMESPACE, "MSFT_TaskTrigger");
            CimInstance cimInstance = new CimInstance(@class);

            JobTriggerToCimInstanceConverter.AddCommonProperties(trigger, cimInstance);
            return(cimInstance);
        }
Exemplo n.º 12
0
 private bool ValidateDailyParams(ScheduledJobTrigger trigger = null)
 {
     if (!base.MyInvocation.BoundParameters.ContainsKey(this._paramDaysInterval) || this._daysInterval >= 1)
     {
         if (!base.MyInvocation.BoundParameters.ContainsKey(this._paramWeeksInterval))
         {
             if (!base.MyInvocation.BoundParameters.ContainsKey(this._paramUser))
             {
                 if (!base.MyInvocation.BoundParameters.ContainsKey(this._paramDaysOfWeek))
                 {
                     if (base.MyInvocation.BoundParameters.ContainsKey(this._paramRepetitionInterval) || base.MyInvocation.BoundParameters.ContainsKey(this._paramRepetitionDuration))
                     {
                         string str = StringUtil.Format(ScheduledJobErrorStrings.InvalidSetTriggerRepetition, ScheduledJobErrorStrings.TriggerDailyType);
                         this.WriteValidationError(str);
                         return(false);
                     }
                     else
                     {
                         if (trigger != null)
                         {
                             DateTime?at = trigger.At;
                             if (!at.HasValue && !base.MyInvocation.BoundParameters.ContainsKey(this._paramAt))
                             {
                                 string str1 = StringUtil.Format(ScheduledJobErrorStrings.MissingAtTime, ScheduledJobErrorStrings.TriggerDailyType);
                                 this.WriteValidationError(str1);
                                 return(false);
                             }
                         }
                         return(true);
                     }
                 }
                 else
                 {
                     string str2 = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysOfWeek, ScheduledJobErrorStrings.TriggerDailyType);
                     this.WriteValidationError(str2);
                     return(false);
                 }
             }
             else
             {
                 string str3 = StringUtil.Format(ScheduledJobErrorStrings.InvalidUser, ScheduledJobErrorStrings.TriggerDailyType);
                 this.WriteValidationError(str3);
                 return(false);
             }
         }
         else
         {
             string str4 = StringUtil.Format(ScheduledJobErrorStrings.InvalidWeeksInterval, ScheduledJobErrorStrings.TriggerDailyType);
             this.WriteValidationError(str4);
             return(false);
         }
     }
     else
     {
         this.WriteValidationError(ScheduledJobErrorStrings.InvalidDaysIntervalParam);
         return(false);
     }
 }
Exemplo n.º 13
0
        private bool ModifyTrigger(ScheduledJobTrigger trigger, TriggerFrequency triggerFrequency, bool validate = false)
        {
            switch (triggerFrequency)
            {
            case TriggerFrequency.AtStartup:
                if (validate &&
                    !ValidateStartupParams())
                {
                    return(false);
                }

                ModifyStartupTrigger(trigger);
                break;

            case TriggerFrequency.AtLogon:
                if (validate &&
                    !ValidateLogonParams())
                {
                    return(false);
                }

                ModifyLogonTrigger(trigger);
                break;

            case TriggerFrequency.Once:
                if (validate &&
                    !ValidateOnceParams())
                {
                    return(false);
                }

                ModifyOnceTrigger(trigger);
                break;

            case TriggerFrequency.Daily:
                if (validate &&
                    !ValidateDailyParams())
                {
                    return(false);
                }

                ModifyDailyTrigger(trigger);
                break;

            case TriggerFrequency.Weekly:
                if (validate &&
                    !ValidateWeeklyParams())
                {
                    return(false);
                }

                ModifyWeeklyTrigger(trigger);
                break;
            }

            return(true);
        }
        private CimInstance ConvertToAtStartup(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass    @class      = cimSession.GetClass(JobTriggerToCimInstanceConverter.CIM_TRIGGER_NAMESPACE, "MSFT_TaskBootTrigger");
            CimInstance cimInstance = new CimInstance(@class);

            cimInstance.CimInstanceProperties["Delay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);
            JobTriggerToCimInstanceConverter.AddCommonProperties(trigger, cimInstance);
            return(cimInstance);
        }
Exemplo n.º 15
0
        private static void AddCommonProperties(ScheduledJobTrigger trigger, CimInstance cimInstance)
        {
            cimInstance.CimInstanceProperties["Enabled"].Value = trigger.Enabled;

            if (trigger.At != null)
            {
                cimInstance.CimInstanceProperties["StartBoundary"].Value = ScheduledJobWTS.ConvertDateTimeToString(trigger.At);
            }
        }
		private static void AddCommonProperties(ScheduledJobTrigger trigger, CimInstance cimInstance)
		{
			cimInstance.CimInstanceProperties["Enabled"].Value = trigger.Enabled;
			DateTime? at = trigger.At;
			if (at.HasValue)
			{
				cimInstance.CimInstanceProperties["StartBoundary"].Value = ScheduledJobWTS.ConvertDateTimeToString(trigger.At);
			}
		}
Exemplo n.º 17
0
        private ScheduledJobTrigger CreateJobTrigger(ITrigger iTrigger)
        {
            TimeSpan            maxValue;
            ScheduledJobTrigger scheduledJobTrigger = null;

            if (iTrigger as IBootTrigger == null)
            {
                if (iTrigger as ILogonTrigger == null)
                {
                    if (iTrigger as ITimeTrigger == null)
                    {
                        if (iTrigger as IDailyTrigger == null)
                        {
                            if (iTrigger as IWeeklyTrigger != null)
                            {
                                IWeeklyTrigger variable = (IWeeklyTrigger)iTrigger;
                                scheduledJobTrigger = ScheduledJobTrigger.CreateWeeklyTrigger(DateTime.Parse(variable.StartBoundary, CultureInfo.InvariantCulture), variable.WeeksInterval, this.ConvertMaskToDaysOfWeekArray(variable.DaysOfWeek), this.ParseWTSTime(variable.RandomDelay), this.ConvertStringId(variable.Id), variable.Enabled);
                            }
                        }
                        else
                        {
                            IDailyTrigger variable1 = (IDailyTrigger)iTrigger;
                            scheduledJobTrigger = ScheduledJobTrigger.CreateDailyTrigger(DateTime.Parse(variable1.StartBoundary, CultureInfo.InvariantCulture), variable1.DaysInterval, this.ParseWTSTime(variable1.RandomDelay), this.ConvertStringId(variable1.Id), variable1.Enabled);
                        }
                    }
                    else
                    {
                        ITimeTrigger variable2 = (ITimeTrigger)iTrigger;
                        TimeSpan     timeSpan  = this.ParseWTSTime(variable2.Repetition.Interval);
                        if (!(timeSpan != TimeSpan.Zero) || variable2.Repetition.StopAtDurationEnd)
                        {
                            maxValue = this.ParseWTSTime(variable2.Repetition.Duration);
                        }
                        else
                        {
                            maxValue = TimeSpan.MaxValue;
                        }
                        TimeSpan timeSpan1 = maxValue;
                        scheduledJobTrigger = ScheduledJobTrigger.CreateOnceTrigger(DateTime.Parse(variable2.StartBoundary, CultureInfo.InvariantCulture), this.ParseWTSTime(variable2.RandomDelay), new TimeSpan?(timeSpan), new TimeSpan?(timeSpan1), this.ConvertStringId(variable2.Id), variable2.Enabled);
                    }
                }
                else
                {
                    ILogonTrigger variable3 = (ILogonTrigger)iTrigger;
                    scheduledJobTrigger = ScheduledJobTrigger.CreateAtLogOnTrigger(variable3.UserId, this.ParseWTSTime(variable3.Delay), this.ConvertStringId(variable3.Id), variable3.Enabled);
                }
            }
            else
            {
                IBootTrigger variable4 = (IBootTrigger)iTrigger;
                scheduledJobTrigger = ScheduledJobTrigger.CreateAtStartupTrigger(this.ParseWTSTime(variable4.Delay), this.ConvertStringId(variable4.Id), variable4.Enabled);
            }
            return(scheduledJobTrigger);
        }
Exemplo n.º 18
0
        private CimInstance ConvertToDaily(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass    cimClass    = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskDailyTrigger");
            CimInstance cimInstance = new CimInstance(cimClass);

            cimInstance.CimInstanceProperties["RandomDelay"].Value  = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);
            cimInstance.CimInstanceProperties["DaysInterval"].Value = trigger.Interval;

            AddCommonProperties(trigger, cimInstance);
            return(cimInstance);
        }
        private CimInstance ConvertToWeekly(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass    @class      = cimSession.GetClass(JobTriggerToCimInstanceConverter.CIM_TRIGGER_NAMESPACE, "MSFT_TaskWeeklyTrigger");
            CimInstance cimInstance = new CimInstance(@class);

            cimInstance.CimInstanceProperties["DaysOfWeek"].Value    = ScheduledJobWTS.ConvertDaysOfWeekToMask(trigger.DaysOfWeek);
            cimInstance.CimInstanceProperties["RandomDelay"].Value   = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);
            cimInstance.CimInstanceProperties["WeeksInterval"].Value = trigger.Interval;
            JobTriggerToCimInstanceConverter.AddCommonProperties(trigger, cimInstance);
            return(cimInstance);
        }
Exemplo n.º 20
0
        private void ModifyLogonTrigger(ScheduledJobTrigger trigger)
        {
            if (MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay)))
            {
                trigger.RandomDelay = _randomDelay;
            }

            if (MyInvocation.BoundParameters.ContainsKey(nameof(User)))
            {
                trigger.User = string.IsNullOrEmpty(_user) ? ScheduledJobTrigger.AllUsers : _user;
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Process input.
        /// </summary>
        protected override void ProcessRecord()
        {
            // Validate the parameter set and write any errors.
            TriggerFrequency newTriggerFrequency = TriggerFrequency.None;

            if (!ValidateParameterSet(ref newTriggerFrequency))
            {
                return;
            }

            // Update each trigger object with the current parameter set.
            // The associated scheduled job definition will also be updated.
            foreach (ScheduledJobTrigger trigger in _triggers)
            {
                ScheduledJobTrigger originalTrigger = new ScheduledJobTrigger(trigger);
                if (!UpdateTrigger(trigger, newTriggerFrequency))
                {
                    continue;
                }

                ScheduledJobDefinition definition = trigger.JobDefinition;
                if (definition != null)
                {
                    bool jobUpdateFailed = false;

                    try
                    {
                        trigger.UpdateJobDefinition();
                    }
                    catch (ScheduledJobException e)
                    {
                        jobUpdateFailed = true;

                        string      msg         = StringUtil.Format(ScheduledJobErrorStrings.CantUpdateTriggerOnJobDef, definition.Name, trigger.Id);
                        Exception   reason      = new RuntimeException(msg, e);
                        ErrorRecord errorRecord = new ErrorRecord(reason, "CantSetPropertiesOnJobTrigger", ErrorCategory.InvalidOperation, trigger);
                        WriteError(errorRecord);
                    }

                    if (jobUpdateFailed)
                    {
                        // Restore trigger to original configuration.
                        originalTrigger.CopyTo(trigger);
                    }
                }

                if (_passThru)
                {
                    WriteObject(trigger);
                }
            }
        }
 protected override void ProcessRecord()
 {
     ScheduledJobTrigger[] scheduledJobTriggerArray = this._triggers;
     for (int i = 0; i < (int)scheduledJobTriggerArray.Length; i++)
     {
         ScheduledJobTrigger enabled = scheduledJobTriggerArray[i];
         enabled.Enabled = this.Enabled;
         if (enabled.JobDefinition != null)
         {
             enabled.UpdateJobDefinition();
         }
     }
 }
Exemplo n.º 23
0
        private void CreateAtStartupTrigger(ScheduledJobTrigger trigger)
        {
            bool     enabled     = trigger.Enabled;
            int      id          = trigger.Id;
            TimeSpan randomDelay = trigger.RandomDelay;

            trigger.ClearProperties();
            trigger.Frequency = TriggerFrequency.AtStartup;
            trigger.Enabled   = enabled;
            trigger.Id        = id;

            trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay)) ? _randomDelay : randomDelay;
        }
Exemplo n.º 24
0
        protected override void ProcessRecord()
        {
            TriggerFrequency triggerFrequency = TriggerFrequency.None;

            if (this.ValidateParameterSet(ref triggerFrequency))
            {
                ScheduledJobTrigger[] scheduledJobTriggerArray = this._triggers;
                for (int i = 0; i < (int)scheduledJobTriggerArray.Length; i++)
                {
                    ScheduledJobTrigger scheduledJobTrigger  = scheduledJobTriggerArray[i];
                    ScheduledJobTrigger scheduledJobTrigger1 = new ScheduledJobTrigger(scheduledJobTrigger);
                    if (this.UpdateTrigger(scheduledJobTrigger, triggerFrequency))
                    {
                        ScheduledJobDefinition jobDefinition = scheduledJobTrigger.JobDefinition;
                        if (jobDefinition != null)
                        {
                            bool flag = false;
                            try
                            {
                                scheduledJobTrigger.UpdateJobDefinition();
                            }
                            catch (ScheduledJobException scheduledJobException1)
                            {
                                ScheduledJobException scheduledJobException = scheduledJobException1;
                                flag = true;
                                object[] name = new object[2];
                                name[0] = jobDefinition.Name;
                                name[1] = scheduledJobTrigger.Id;
                                string      str = StringUtil.Format(ScheduledJobErrorStrings.CantUpdateTriggerOnJobDef, name);
                                Exception   runtimeException = new RuntimeException(str, scheduledJobException);
                                ErrorRecord errorRecord      = new ErrorRecord(runtimeException, "CantSetPropertiesOnJobTrigger", ErrorCategory.InvalidOperation, scheduledJobTrigger);
                                base.WriteError(errorRecord);
                            }
                            if (flag)
                            {
                                scheduledJobTrigger1.CopyTo(scheduledJobTrigger);
                            }
                        }
                        if (this._passThru)
                        {
                            base.WriteObject(scheduledJobTrigger);
                        }
                    }
                }
                return;
            }
            else
            {
                return;
            }
        }
Exemplo n.º 25
0
        private bool ValidateWeeklyParams(ScheduledJobTrigger trigger = null)
        {
            if (MyInvocation.BoundParameters.ContainsKey(nameof(DaysInterval)))
            {
                string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidDaysInterval, ScheduledJobErrorStrings.TriggerWeeklyType);
                WriteValidationError(msg);
                return(false);
            }

            if (MyInvocation.BoundParameters.ContainsKey(nameof(WeeksInterval)) &&
                _weeksInterval < 1)
            {
                WriteValidationError(ScheduledJobErrorStrings.InvalidWeeksIntervalParam);
                return(false);
            }

            if (MyInvocation.BoundParameters.ContainsKey(nameof(User)))
            {
                string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidUser, ScheduledJobErrorStrings.TriggerWeeklyType);
                WriteValidationError(msg);
                return(false);
            }

            if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)) ||
                MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInfiniteDuration)))
            {
                string msg = StringUtil.Format(ScheduledJobErrorStrings.InvalidSetTriggerRepetition, ScheduledJobErrorStrings.TriggerWeeklyType);
                WriteValidationError(msg);
                return(false);
            }

            if (trigger != null)
            {
                if (trigger.At == null && !MyInvocation.BoundParameters.ContainsKey(nameof(At)))
                {
                    string msg = StringUtil.Format(ScheduledJobErrorStrings.MissingAtTime, ScheduledJobErrorStrings.TriggerDailyType);
                    WriteValidationError(msg);
                    return(false);
                }

                if ((trigger.DaysOfWeek == null || trigger.DaysOfWeek.Count == 0) &&
                    !MyInvocation.BoundParameters.ContainsKey(nameof(DaysOfWeek)))
                {
                    string msg = StringUtil.Format(ScheduledJobErrorStrings.MissingDaysOfWeek, ScheduledJobErrorStrings.TriggerDailyType);
                    WriteValidationError(msg);
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 26
0
        private void CreateDailyTrigger(ScheduledJobTrigger trigger)
        {
            TimeSpan timeSpan;
            DateTime?nullable;
            int      num;
            bool     enabled     = trigger.Enabled;
            int      id          = trigger.Id;
            TimeSpan randomDelay = trigger.RandomDelay;
            DateTime?at          = trigger.At;
            int      interval    = trigger.Interval;

            trigger.ClearProperties();
            trigger.Frequency = TriggerFrequency.Daily;
            trigger.Enabled   = enabled;
            trigger.Id        = id;
            ScheduledJobTrigger scheduledJobTrigger = trigger;

            if (base.MyInvocation.BoundParameters.ContainsKey(this._paramRandomDelay))
            {
                timeSpan = this._randomDelay;
            }
            else
            {
                timeSpan = randomDelay;
            }
            scheduledJobTrigger.RandomDelay = timeSpan;
            ScheduledJobTrigger scheduledJobTrigger1 = trigger;

            if (base.MyInvocation.BoundParameters.ContainsKey(this._paramAt))
            {
                nullable = new DateTime?(this._atTime);
            }
            else
            {
                nullable = at;
            }
            scheduledJobTrigger1.At = nullable;
            ScheduledJobTrigger scheduledJobTrigger2 = trigger;

            if (base.MyInvocation.BoundParameters.ContainsKey(this._paramDaysInterval))
            {
                num = this._daysInterval;
            }
            else
            {
                num = interval;
            }
            scheduledJobTrigger2.Interval = num;
        }
Exemplo n.º 27
0
 private void ModifyDailyTrigger(ScheduledJobTrigger trigger)
 {
     if (base.MyInvocation.BoundParameters.ContainsKey(this._paramRandomDelay))
     {
         trigger.RandomDelay = this._randomDelay;
     }
     if (base.MyInvocation.BoundParameters.ContainsKey(this._paramAt))
     {
         trigger.At = new DateTime?(this._atTime);
     }
     if (base.MyInvocation.BoundParameters.ContainsKey(this._paramDaysInterval))
     {
         trigger.Interval = this._daysInterval;
     }
 }
Exemplo n.º 28
0
 public void UpdateJobDefinition()
 {
     if (this._jobDefAssociation != null)
     {
         ScheduledJobTrigger[] scheduledJobTriggerArray = new ScheduledJobTrigger[1];
         scheduledJobTriggerArray[0] = this;
         this._jobDefAssociation.UpdateTriggers(scheduledJobTriggerArray, true);
         return;
     }
     else
     {
         string str = StringUtil.Format(ScheduledJobErrorStrings.NoAssociatedJobDefinitionForTrigger, this._id);
         throw new RuntimeException(str);
     }
 }
Exemplo n.º 29
0
        private void CreateAtLogonTrigger(ScheduledJobTrigger trigger)
        {
            bool     enabled     = trigger.Enabled;
            int      id          = trigger.Id;
            TimeSpan randomDelay = trigger.RandomDelay;
            string   user        = string.IsNullOrEmpty(trigger.User) ? ScheduledJobTrigger.AllUsers : trigger.User;

            trigger.ClearProperties();
            trigger.Frequency = TriggerFrequency.AtLogon;
            trigger.Enabled   = enabled;
            trigger.Id        = id;

            trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(nameof(RandomDelay)) ? _randomDelay : randomDelay;
            trigger.User        = MyInvocation.BoundParameters.ContainsKey(nameof(User)) ? _user : user;
        }
Exemplo n.º 30
0
        private CimInstance ConvertToAtLogon(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass    cimClass    = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskLogonTrigger");
            CimInstance cimInstance = new CimInstance(cimClass);

            cimInstance.CimInstanceProperties["Delay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);

            // Convert the "AllUsers" name ("*" character) to null for Task Scheduler.
            string userId = (ScheduledJobTrigger.IsAllUsers(trigger.User)) ? null : trigger.User;

            cimInstance.CimInstanceProperties["UserId"].Value = userId;

            AddCommonProperties(trigger, cimInstance);
            return(cimInstance);
        }
Exemplo n.º 31
0
        private void CreateOnceTrigger()
        {
            TimeSpan?repInterval = null;
            TimeSpan?repDuration = null;

            if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)) ||
                MyInvocation.BoundParameters.ContainsKey(nameof(RepeatIndefinitely)))
            {
                if (MyInvocation.BoundParameters.ContainsKey(nameof(RepeatIndefinitely)))
                {
                    if (MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)))
                    {
                        throw new PSArgumentException(ScheduledJobErrorStrings.InvalidRepeatIndefinitelyParams);
                    }

                    if (!MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)))
                    {
                        throw new PSArgumentException(ScheduledJobErrorStrings.InvalidRepetitionRepeatParams);
                    }

                    _repDuration = TimeSpan.MaxValue;
                }
                else if (!MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionInterval)) || !MyInvocation.BoundParameters.ContainsKey(nameof(RepetitionDuration)))
                {
                    throw new PSArgumentException(ScheduledJobErrorStrings.InvalidRepetitionParams);
                }

                if (_repInterval < TimeSpan.Zero || _repDuration < TimeSpan.Zero)
                {
                    throw new PSArgumentException(ScheduledJobErrorStrings.InvalidRepetitionParamValues);
                }

                if (_repInterval < TimeSpan.FromMinutes(1))
                {
                    throw new PSArgumentException(ScheduledJobErrorStrings.InvalidRepetitionIntervalValue);
                }

                if (_repInterval > _repDuration)
                {
                    throw new PSArgumentException(ScheduledJobErrorStrings.InvalidRepetitionInterval);
                }

                repInterval = _repInterval;
                repDuration = _repDuration;
            }

            WriteObject(ScheduledJobTrigger.CreateOnceTrigger(_atTime, _randomDelay, repInterval, repDuration, 0, true));
        }
		private CimInstance ConvertToAtLogon(ScheduledJobTrigger trigger, CimSession cimSession)
		{
			string user;
			CimClass @class = cimSession.GetClass(JobTriggerToCimInstanceConverter.CIM_TRIGGER_NAMESPACE, "MSFT_TaskLogonTrigger");
			CimInstance cimInstance = new CimInstance(@class);
			cimInstance.CimInstanceProperties["Delay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);
			if (ScheduledJobTrigger.IsAllUsers(trigger.User))
			{
				user = null;
			}
			else
			{
				user = trigger.User;
			}
			string str = user;
			cimInstance.CimInstanceProperties["UserId"].Value = str;
			JobTriggerToCimInstanceConverter.AddCommonProperties(trigger, cimInstance);
			return cimInstance;
		}
		private CimInstance ConvertToOnce(ScheduledJobTrigger trigger, CimSession cimSession)
		{
			bool valueOrDefault;
			CimClass @class = cimSession.GetClass(JobTriggerToCimInstanceConverter.CIM_TRIGGER_NAMESPACE, "MSFT_TaskTimeTrigger");
			CimInstance cimInstance = new CimInstance(@class);
			cimInstance.CimInstanceProperties["RandomDelay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);
			TimeSpan? repetitionInterval = trigger.RepetitionInterval;
			if (repetitionInterval.HasValue)
			{
				TimeSpan? repetitionDuration = trigger.RepetitionDuration;
				if (repetitionDuration.HasValue)
				{
					CimClass cimClass = cimSession.GetClass(JobTriggerToCimInstanceConverter.CIM_TRIGGER_NAMESPACE, "MSFT_TaskRepetitionPattern");
					CimInstance wTSString = new CimInstance(cimClass);
					TimeSpan? nullable = trigger.RepetitionInterval;
					wTSString.CimInstanceProperties["Interval"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(nullable.Value);
					TimeSpan? repetitionDuration1 = trigger.RepetitionDuration;
					TimeSpan maxValue = TimeSpan.MaxValue;
					if (!repetitionDuration1.HasValue)
					{
						valueOrDefault = false;
					}
					else
					{
						valueOrDefault = repetitionDuration1.GetValueOrDefault() == maxValue;
					}
					if (!valueOrDefault)
					{
						wTSString.CimInstanceProperties["StopAtDurationEnd"].Value = true;
						TimeSpan? nullable1 = trigger.RepetitionDuration;
						wTSString.CimInstanceProperties["Duration"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(nullable1.Value);
					}
					else
					{
						wTSString.CimInstanceProperties["StopAtDurationEnd"].Value = false;
					}
					cimInstance.CimInstanceProperties["Repetition"].Value = wTSString;
				}
			}
			JobTriggerToCimInstanceConverter.AddCommonProperties(trigger, cimInstance);
			return cimInstance;
		}
Exemplo n.º 34
0
        private bool CreateTrigger(ScheduledJobTrigger trigger, TriggerFrequency triggerFrequency)
        {
            switch (triggerFrequency)
            {
                case TriggerFrequency.AtStartup:
                    CreateAtStartupTrigger(trigger);
                    break;

                case TriggerFrequency.AtLogon:
                    CreateAtLogonTrigger(trigger);
                    break;

                case TriggerFrequency.Once:
                    if (trigger.Frequency != triggerFrequency &&
                        !ValidateOnceParams(trigger))
                    {
                        return false;
                    }
                    CreateOnceTrigger(trigger);
                    break;

                case TriggerFrequency.Daily:
                    if (trigger.Frequency != triggerFrequency &&
                        !ValidateDailyParams(trigger))
                    {
                        return false;
                    }
                    CreateDailyTrigger(trigger);
                    break;

                case TriggerFrequency.Weekly:
                    if (trigger.Frequency != triggerFrequency &&
                        !ValidateWeeklyParams(trigger))
                    {
                        return false;
                    }
                    CreateWeeklyTrigger(trigger);
                    break;
            }

            return true;
        }
Exemplo n.º 35
0
 private void ModifyStartupTrigger(ScheduledJobTrigger trigger)
 {
     if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay))
     {
         trigger.RandomDelay = _randomDelay;
     }
 }
Exemplo n.º 36
0
        internal void CopyTo(ScheduledJobTrigger targetTrigger)
        {
            if (targetTrigger == null)
            {
                throw new PSArgumentNullException("targetTrigger");
            }

            targetTrigger.Enabled = _enabled;
            targetTrigger.Frequency = _frequency;
            targetTrigger.Id = _id;
            targetTrigger.At = _time;
            targetTrigger.DaysOfWeek = _daysOfWeek;
            targetTrigger.Interval = _interval;
            targetTrigger.RandomDelay = _randomDelay;
            targetTrigger.RepetitionInterval = _repInterval;
            targetTrigger.RepetitionDuration = _repDuration;
            targetTrigger.User = _user;
            targetTrigger.JobDefinition = _jobDefAssociation;
        }
Exemplo n.º 37
0
        private void ModifyWeeklyTrigger(ScheduledJobTrigger trigger)
        {
            if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay))
            {
                trigger.RandomDelay = _randomDelay;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramAt))
            {
                trigger.At = _atTime;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval))
            {
                trigger.Interval = _weeksInterval;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek))
            {
                trigger.DaysOfWeek = new List<DayOfWeek>(_daysOfWeek);
            }
        }
Exemplo n.º 38
0
        private void CreateAtStartupTrigger(ScheduledJobTrigger trigger)
        {
            bool enabled = trigger.Enabled;
            int id = trigger.Id;
            TimeSpan randomDelay = trigger.RandomDelay;

            trigger.ClearProperties();
            trigger.Frequency = TriggerFrequency.AtStartup;
            trigger.Enabled = enabled;
            trigger.Id = id;

            trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay) ? _randomDelay : randomDelay;
        }
		private CimInstance ConvertToDaily(ScheduledJobTrigger trigger, CimSession cimSession)
		{
			CimClass @class = cimSession.GetClass(JobTriggerToCimInstanceConverter.CIM_TRIGGER_NAMESPACE, "MSFT_TaskDailyTrigger");
			CimInstance cimInstance = new CimInstance(@class);
			cimInstance.CimInstanceProperties["RandomDelay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);
			cimInstance.CimInstanceProperties["DaysInterval"].Value = trigger.Interval;
			JobTriggerToCimInstanceConverter.AddCommonProperties(trigger, cimInstance);
			return cimInstance;
		}
Exemplo n.º 40
0
        private CimInstance ConvertToAtStartup(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass cimClass = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskBootTrigger");
            CimInstance cimInstance = new CimInstance(cimClass);

            cimInstance.CimInstanceProperties["Delay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);

            AddCommonProperties(trigger, cimInstance);
            return cimInstance;
        }
Exemplo n.º 41
0
 private CimInstance ConvertToDefault(ScheduledJobTrigger trigger, CimSession cimSession)
 {
     CimClass cimClass = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskTrigger");
     CimInstance result = new CimInstance(cimClass);
     AddCommonProperties(trigger, result);
     return result;
 }
Exemplo n.º 42
0
        private CimInstance ConvertToAtLogon(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass cimClass = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskLogonTrigger");
            CimInstance cimInstance = new CimInstance(cimClass);

            cimInstance.CimInstanceProperties["Delay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);

            // Convert the "AllUsers" name ("*" character) to null for Task Scheduler.
            string userId = (ScheduledJobTrigger.IsAllUsers(trigger.User)) ? null : trigger.User;
            cimInstance.CimInstanceProperties["UserId"].Value = userId;

            AddCommonProperties(trigger, cimInstance);
            return cimInstance;
        }
Exemplo n.º 43
0
        private CimInstance ConvertToOnce(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass cimClass = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskTimeTrigger");
            CimInstance cimInstance = new CimInstance(cimClass);

            cimInstance.CimInstanceProperties["RandomDelay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);

            if (trigger.RepetitionInterval != null && trigger.RepetitionDuration != null)
            {
                CimClass cimRepClass = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskRepetitionPattern");
                CimInstance cimRepInstance = new CimInstance(cimRepClass);

                cimRepInstance.CimInstanceProperties["Interval"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RepetitionInterval.Value);

                if (trigger.RepetitionDuration == TimeSpan.MaxValue)
                {
                    cimRepInstance.CimInstanceProperties["StopAtDurationEnd"].Value = false;
                }
                else
                {
                    cimRepInstance.CimInstanceProperties["StopAtDurationEnd"].Value = true;
                    cimRepInstance.CimInstanceProperties["Duration"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RepetitionDuration.Value);
                }

                cimInstance.CimInstanceProperties["Repetition"].Value = cimRepInstance;
            }

            AddCommonProperties(trigger, cimInstance);
            return cimInstance;
        }
Exemplo n.º 44
0
        private CimInstance ConvertToWeekly(ScheduledJobTrigger trigger, CimSession cimSession)
        {
            CimClass cimClass = cimSession.GetClass(CIM_TRIGGER_NAMESPACE, "MSFT_TaskWeeklyTrigger");
            CimInstance cimInstance = new CimInstance(cimClass);

            cimInstance.CimInstanceProperties["DaysOfWeek"].Value = ScheduledJobWTS.ConvertDaysOfWeekToMask(trigger.DaysOfWeek);
            cimInstance.CimInstanceProperties["RandomDelay"].Value = ScheduledJobWTS.ConvertTimeSpanToWTSString(trigger.RandomDelay);
            cimInstance.CimInstanceProperties["WeeksInterval"].Value = trigger.Interval;

            AddCommonProperties(trigger, cimInstance);
            return cimInstance;
        }
Exemplo n.º 45
0
        private void CreateWeeklyTrigger(ScheduledJobTrigger trigger)
        {
            bool enabled = trigger.Enabled;
            int id = trigger.Id;
            TimeSpan randomDelay = trigger.RandomDelay;
            DateTime? atTime = trigger.At;
            int interval = trigger.Interval;
            List<DayOfWeek> daysOfWeek = trigger.DaysOfWeek;

            trigger.ClearProperties();
            trigger.Frequency = TriggerFrequency.Weekly;
            trigger.Enabled = enabled;
            trigger.Id = id;

            trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay) ? _randomDelay : randomDelay;
            trigger.At = MyInvocation.BoundParameters.ContainsKey(_paramAt) ? _atTime : atTime;
            trigger.Interval = MyInvocation.BoundParameters.ContainsKey(_paramWeeksInterval) ? _weeksInterval : interval;
            trigger.DaysOfWeek = MyInvocation.BoundParameters.ContainsKey(_paramDaysOfWeek) ? new List<DayOfWeek>(_daysOfWeek) : daysOfWeek;
        }
Exemplo n.º 46
0
        /// <summary>
        /// Adds new ScheduledJobTriggers.
        /// </summary>
        /// <param name="triggers">Collection of ScheduledJobTrigger objects</param>
        /// <param name="save">Update Windows Task Scheduler and save to store</param>
        public void AddTriggers(
            IEnumerable<ScheduledJobTrigger> triggers,
            bool save)
        {
            IsDisposed();

            if (triggers == null)
            {
                throw new PSArgumentNullException("triggers");
            }

            // First validate all triggers.
            ValidateTriggers(triggers);

            Collection<int> newTriggerIds = new Collection<int>();
            foreach (ScheduledJobTrigger trigger in triggers)
            {
                ScheduledJobTrigger newTrigger = new ScheduledJobTrigger(trigger);

                newTrigger.Id = ++_currentTriggerId;
                newTriggerIds.Add(newTrigger.Id);
                newTrigger.JobDefinition = this;
                _triggers.Add(newTrigger.Id, newTrigger);
            }

            if (save)
            {
                Save();
            }
        }
Exemplo n.º 47
0
        private void CreateOnceTrigger(ScheduledJobTrigger trigger)
        {
            bool enabled = trigger.Enabled;
            int id = trigger.Id;
            TimeSpan randomDelay = trigger.RandomDelay;
            DateTime? atTime = trigger.At;
            TimeSpan? repInterval = trigger.RepetitionInterval;
            TimeSpan? repDuration = trigger.RepetitionDuration;

            trigger.ClearProperties();
            trigger.Frequency = TriggerFrequency.Once;
            trigger.Enabled = enabled;
            trigger.Id = id;

            trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay) ? _randomDelay : randomDelay;
            trigger.At = MyInvocation.BoundParameters.ContainsKey(_paramAt) ? _atTime : atTime;
            trigger.RepetitionInterval = MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval) ? _repInterval : repInterval;
            trigger.RepetitionDuration = MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration) ? _repDuration : repDuration;
        }
Exemplo n.º 48
0
        private void ModifyDailyTrigger(ScheduledJobTrigger trigger)
        {
            if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay))
            {
                trigger.RandomDelay = _randomDelay;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramAt))
            {
                trigger.At = _atTime;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramDaysInterval))
            {
                trigger.Interval = _daysInterval;
            }
        }
Exemplo n.º 49
0
        private void CreateAtLogonTrigger(ScheduledJobTrigger trigger)
        {
            bool enabled = trigger.Enabled;
            int id = trigger.Id;
            TimeSpan randomDelay = trigger.RandomDelay;
            string user = string.IsNullOrEmpty(trigger.User) ? ScheduledJobTrigger.AllUsers : trigger.User;

            trigger.ClearProperties();
            trigger.Frequency = TriggerFrequency.AtLogon;
            trigger.Enabled = enabled;
            trigger.Id = id;

            trigger.RandomDelay = MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay) ? _randomDelay : randomDelay;
            trigger.User = MyInvocation.BoundParameters.ContainsKey(_paramUser) ? _user : user;
        }
Exemplo n.º 50
0
        private void ModifyLogonTrigger(ScheduledJobTrigger trigger)
        {
            if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay))
            {
                trigger.RandomDelay = _randomDelay;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramUser))
            {
                trigger.User = string.IsNullOrEmpty(_user) ? ScheduledJobTrigger.AllUsers : _user;
            }
        }
Exemplo n.º 51
0
        /// <summary>
        /// Updates triggers with provided trigger objects, matching passed in
        /// trigger Id with existing trigger Id.
        /// </summary>
        /// <param name="triggers">Collection of ScheduledJobTrigger objects to update</param>
        /// <param name="save">Update Windows Task Scheduler and save to store</param>
        /// <returns>Trigger Ids not found.</returns>
        public List<Int32> UpdateTriggers(
            IEnumerable<ScheduledJobTrigger> triggers,
            bool save)
        {
            IsDisposed();

            if (triggers == null)
            {
                throw new PSArgumentNullException("triggers");
            }

            // First validate all triggers.
            ValidateTriggers(triggers);

            List<Int32> idsNotFound = new List<Int32>();
            bool triggerFound = false;
            foreach (ScheduledJobTrigger updateTrigger in triggers)
            {
                if (_triggers.ContainsKey(updateTrigger.Id))
                {
                    // Disassociate old trigger from this definition.
                    _triggers[updateTrigger.Id].JobDefinition = null;

                    // Replace older trigger object with new updated one.
                    ScheduledJobTrigger newTrigger = new ScheduledJobTrigger(updateTrigger);
                    newTrigger.Id = updateTrigger.Id;
                    newTrigger.JobDefinition = this;
                    _triggers[newTrigger.Id] = newTrigger;
                    triggerFound = true;
                }
                else
                {
                    idsNotFound.Add(updateTrigger.Id);
                }
            }

            if (save && triggerFound)
            {
                Save();
            }

            return idsNotFound;
        }
		private CimInstance ConvertToDefault(ScheduledJobTrigger trigger, CimSession cimSession)
		{
			CimClass @class = cimSession.GetClass(JobTriggerToCimInstanceConverter.CIM_TRIGGER_NAMESPACE, "MSFT_TaskTrigger");
			CimInstance cimInstance = new CimInstance(@class);
			JobTriggerToCimInstanceConverter.AddCommonProperties(trigger, cimInstance);
			return cimInstance;
		}
Exemplo n.º 53
0
        private void ModifyOnceTrigger(ScheduledJobTrigger trigger)
        {
            if (MyInvocation.BoundParameters.ContainsKey(_paramRandomDelay))
            {
                trigger.RandomDelay = _randomDelay;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionInterval))
            {
                trigger.RepetitionInterval = _repInterval;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramRepetitionDuration))
            {
                trigger.RepetitionDuration = _repDuration;
            }

            if (MyInvocation.BoundParameters.ContainsKey(_paramAt))
            {
                trigger.At = _atTime;
            }
        }
Exemplo n.º 54
0
        /// <summary>
        /// Creates a new set of ScheduledJobTriggers for this object.
        /// </summary>
        /// <param name="newTriggers">Array of ScheduledJobTrigger objects to set</param>
        /// <param name="save">Update Windows Task Scheduler and save to store</param>
        public void SetTriggers(
            IEnumerable<ScheduledJobTrigger> newTriggers,
            bool save)
        {
            IsDisposed();

            // First validate all triggers.
            ValidateTriggers(newTriggers);

            // Disassociate any old trigger objects from this definition.
            foreach (ScheduledJobTrigger trigger in _triggers.Values)
            {
                trigger.JobDefinition = null;
            }

            _currentTriggerId = 0;
            _triggers = new Dictionary<Int32, ScheduledJobTrigger>();
            if (newTriggers != null)
            {
                foreach (ScheduledJobTrigger trigger in newTriggers)
                {
                    ScheduledJobTrigger newTrigger = new ScheduledJobTrigger(trigger);

                    newTrigger.Id = ++_currentTriggerId;
                    newTrigger.JobDefinition = this;
                    _triggers.Add(newTrigger.Id, newTrigger);
                }
            }

            if (save)
            {
                Save();
            }
        }
Exemplo n.º 55
0
		public void SetTriggers(IEnumerable<ScheduledJobTrigger> newTriggers, bool save)
		{
			this.IsDisposed();
			this.ValidateTriggers(newTriggers);
			foreach (ScheduledJobTrigger value in this._triggers.Values)
			{
				value.JobDefinition = null;
			}
			this._currentTriggerId = 0;
			this._triggers = new Dictionary<int, ScheduledJobTrigger>();
			if (newTriggers != null)
			{
				foreach (ScheduledJobTrigger newTrigger in newTriggers)
				{
					ScheduledJobTrigger scheduledJobTrigger = new ScheduledJobTrigger(newTrigger);
					ScheduledJobDefinition scheduledJobDefinition = this;
					int num = scheduledJobDefinition._currentTriggerId + 1;
					int num1 = num;
					scheduledJobDefinition._currentTriggerId = num;
					scheduledJobTrigger.Id = num1;
					scheduledJobTrigger.JobDefinition = this;
					this._triggers.Add(scheduledJobTrigger.Id, scheduledJobTrigger);
				}
			}
			if (save)
			{
				this.Save();
			}
		}
Exemplo n.º 56
0
		public List<int> UpdateTriggers(IEnumerable<ScheduledJobTrigger> triggers, bool save)
		{
			this.IsDisposed();
			if (triggers != null)
			{
				this.ValidateTriggers(triggers);
				List<int> nums = new List<int>();
				bool flag = false;
				foreach (ScheduledJobTrigger trigger in triggers)
				{
					if (!this._triggers.ContainsKey(trigger.Id))
					{
						nums.Add(trigger.Id);
					}
					else
					{
						this._triggers[trigger.Id].JobDefinition = null;
						ScheduledJobTrigger scheduledJobTrigger = new ScheduledJobTrigger(trigger);
						scheduledJobTrigger.Id = trigger.Id;
						scheduledJobTrigger.JobDefinition = this;
						this._triggers[scheduledJobTrigger.Id] = scheduledJobTrigger;
						flag = true;
					}
				}
				if (save && flag)
				{
					this.Save();
				}
				return nums;
			}
			else
			{
				throw new PSArgumentNullException("triggers");
			}
		}
Exemplo n.º 57
0
        private bool ModifyTrigger(ScheduledJobTrigger trigger, TriggerFrequency triggerFrequency, bool validate = false)
        {
            switch (triggerFrequency)
            {
                case TriggerFrequency.AtStartup:
                    if (validate &&
                        !ValidateStartupParams())
                    {
                        return false;
                    }
                    ModifyStartupTrigger(trigger);
                    break;

                case TriggerFrequency.AtLogon:
                    if (validate &&
                        !ValidateLogonParams())
                    {
                        return false;
                    }
                    ModifyLogonTrigger(trigger);
                    break;

                case TriggerFrequency.Once:
                    if (validate &&
                        !ValidateOnceParams())
                    {
                        return false;
                    }
                    ModifyOnceTrigger(trigger);
                    break;

                case TriggerFrequency.Daily:
                    if (validate &&
                        !ValidateDailyParams())
                    {
                        return false;
                    }
                    ModifyDailyTrigger(trigger);
                    break;

                case TriggerFrequency.Weekly:
                    if (validate &&
                        !ValidateWeeklyParams())
                    {
                        return false;
                    }
                    ModifyWeeklyTrigger(trigger);
                    break;
            }

            return true;
        }
Exemplo n.º 58
0
		public void AddTriggers(IEnumerable<ScheduledJobTrigger> triggers, bool save)
		{
			this.IsDisposed();
			if (triggers != null)
			{
				this.ValidateTriggers(triggers);
				Collection<int> nums = new Collection<int>();
				foreach (ScheduledJobTrigger trigger in triggers)
				{
					ScheduledJobTrigger scheduledJobTrigger = new ScheduledJobTrigger(trigger);
					ScheduledJobDefinition scheduledJobDefinition = this;
					int num = scheduledJobDefinition._currentTriggerId + 1;
					int num1 = num;
					scheduledJobDefinition._currentTriggerId = num;
					scheduledJobTrigger.Id = num1;
					nums.Add(scheduledJobTrigger.Id);
					scheduledJobTrigger.JobDefinition = this;
					this._triggers.Add(scheduledJobTrigger.Id, scheduledJobTrigger);
				}
				if (save)
				{
					this.Save();
				}
				return;
			}
			else
			{
				throw new PSArgumentNullException("triggers");
			}
		}
Exemplo n.º 59
0
        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="copyTrigger">ScheduledJobTrigger</param>
        internal ScheduledJobTrigger(ScheduledJobTrigger copyTrigger)
        {
            if (copyTrigger == null)
            {
                throw new PSArgumentNullException("copyTrigger");
            }

            _enabled = copyTrigger.Enabled;
            _frequency = copyTrigger.Frequency;
            _id = copyTrigger.Id;
            _time = copyTrigger.At;
            _daysOfWeek = copyTrigger.DaysOfWeek;
            _interval = copyTrigger.Interval;
            _randomDelay = copyTrigger.RandomDelay;
            _repInterval = copyTrigger.RepetitionInterval;
            _repDuration = copyTrigger.RepetitionDuration;
            _user = copyTrigger.User;

            _jobDefAssociation = copyTrigger.JobDefinition;
        }
Exemplo n.º 60
0
        /// <summary>
        /// Creates a new WTS trigger based on the provided ScheduledJobTrigger object
        /// and adds it to the provided ITaskDefinition object.
        /// </summary>
        /// <param name="iTaskDefinition">ITaskDefinition</param>
        /// <param name="jobTrigger">ScheduledJobTrigger</param>
        private void AddTaskTrigger(
            ITaskDefinition iTaskDefinition,
            ScheduledJobTrigger jobTrigger)
        {
            ITrigger iTrigger = null;

            switch (jobTrigger.Frequency)
            {
                case TriggerFrequency.AtStartup:
                    {
                        iTrigger = iTaskDefinition.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_BOOT);
                        IBootTrigger iBootTrigger = iTrigger as IBootTrigger;
                        Debug.Assert(iBootTrigger != null);

                        iBootTrigger.Delay = ConvertTimeSpanToWTSString(jobTrigger.RandomDelay);

                        iTrigger.Id = jobTrigger.Id.ToString(CultureInfo.InvariantCulture);
                        iTrigger.Enabled = jobTrigger.Enabled;
                    }
                    break;

                case TriggerFrequency.AtLogon:
                    {
                        iTrigger = iTaskDefinition.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
                        ILogonTrigger iLogonTrigger = iTrigger as ILogonTrigger;
                        Debug.Assert(iLogonTrigger != null);

                        iLogonTrigger.UserId = ScheduledJobTrigger.IsAllUsers(jobTrigger.User) ? null : jobTrigger.User;
                        iLogonTrigger.Delay = ConvertTimeSpanToWTSString(jobTrigger.RandomDelay);

                        iTrigger.Id = jobTrigger.Id.ToString(CultureInfo.InvariantCulture);
                        iTrigger.Enabled = jobTrigger.Enabled;
                    }
                    break;

                case TriggerFrequency.Once:
                    {
                        iTrigger = iTaskDefinition.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
                        ITimeTrigger iTimeTrigger = iTrigger as ITimeTrigger;
                        Debug.Assert(iTimeTrigger != null);

                        iTimeTrigger.RandomDelay = ConvertTimeSpanToWTSString(jobTrigger.RandomDelay);

                        // Time trigger repetition.
                        if (jobTrigger.RepetitionInterval != null &&
                            jobTrigger.RepetitionDuration != null)
                        {
                            iTimeTrigger.Repetition.Interval = ConvertTimeSpanToWTSString(jobTrigger.RepetitionInterval.Value);
                            if (jobTrigger.RepetitionDuration.Value == TimeSpan.MaxValue)
                            {
                                iTimeTrigger.Repetition.StopAtDurationEnd = false;
                            }
                            else
                            {
                                iTimeTrigger.Repetition.StopAtDurationEnd = true;
                                iTimeTrigger.Repetition.Duration = ConvertTimeSpanToWTSString(jobTrigger.RepetitionDuration.Value);
                            }
                        }

                        iTrigger.StartBoundary = ConvertDateTimeToString(jobTrigger.At);
                        iTrigger.Id = jobTrigger.Id.ToString(CultureInfo.InvariantCulture);
                        iTrigger.Enabled = jobTrigger.Enabled;
                    }
                    break;

                case TriggerFrequency.Daily:
                    {
                        iTrigger = iTaskDefinition.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
                        IDailyTrigger iDailyTrigger = iTrigger as IDailyTrigger;
                        Debug.Assert(iDailyTrigger != null);

                        iDailyTrigger.RandomDelay = ConvertTimeSpanToWTSString(jobTrigger.RandomDelay);
                        iDailyTrigger.DaysInterval = (short)jobTrigger.Interval;

                        iTrigger.StartBoundary = ConvertDateTimeToString(jobTrigger.At);
                        iTrigger.Id = jobTrigger.Id.ToString(CultureInfo.InvariantCulture);
                        iTrigger.Enabled = jobTrigger.Enabled;
                    }
                    break;

                case TriggerFrequency.Weekly:
                    {
                        iTrigger = iTaskDefinition.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_WEEKLY);
                        IWeeklyTrigger iWeeklyTrigger = iTrigger as IWeeklyTrigger;
                        Debug.Assert(iWeeklyTrigger != null);

                        iWeeklyTrigger.RandomDelay = ConvertTimeSpanToWTSString(jobTrigger.RandomDelay);
                        iWeeklyTrigger.WeeksInterval = (short)jobTrigger.Interval;
                        iWeeklyTrigger.DaysOfWeek = ConvertDaysOfWeekToMask(jobTrigger.DaysOfWeek);

                        iTrigger.StartBoundary = ConvertDateTimeToString(jobTrigger.At);
                        iTrigger.Id = jobTrigger.Id.ToString(CultureInfo.InvariantCulture);
                        iTrigger.Enabled = jobTrigger.Enabled;
                    }
                    break;
            }
        }