internal ScheduledJobOptions(ScheduledJobOptions copyOptions) { if (copyOptions != null) { this._startIfOnBatteries = copyOptions.StartIfOnBatteries; this._stopIfGoingOnBatteries = copyOptions.StopIfGoingOnBatteries; this._wakeToRun = copyOptions.WakeToRun; this._startIfNotIdle = copyOptions.StartIfNotIdle; this._stopIfGoingOffIdle = copyOptions.StopIfGoingOffIdle; this._restartOnIdleResume = copyOptions.RestartOnIdleResume; this._idleDuration = copyOptions.IdleDuration; this._idleTimeout = copyOptions.IdleTimeout; this._showInTaskScheduler = copyOptions.ShowInTaskScheduler; this._runElevated = copyOptions.RunElevated; this._runWithoutNetwork = copyOptions.RunWithoutNetwork; this._donotAllowDemandStart = copyOptions.DoNotAllowDemandStart; this._multipleInstancePolicy = copyOptions.MultipleInstancePolicy; this._jobDefAssociation = copyOptions.JobDefinition; return; } else { throw new PSArgumentNullException("copyOptions"); } }
private void AddTaskOptions(ITaskDefinition iTaskDefinition, ScheduledJobOptions jobOptions) { _TASK_RUNLEVEL variable; iTaskDefinition.Settings.DisallowStartIfOnBatteries = !jobOptions.StartIfOnBatteries; iTaskDefinition.Settings.StopIfGoingOnBatteries = jobOptions.StopIfGoingOnBatteries; iTaskDefinition.Settings.WakeToRun = jobOptions.WakeToRun; iTaskDefinition.Settings.RunOnlyIfIdle = !jobOptions.StartIfNotIdle; iTaskDefinition.Settings.IdleSettings.StopOnIdleEnd = jobOptions.StopIfGoingOffIdle; iTaskDefinition.Settings.IdleSettings.RestartOnIdle = jobOptions.RestartOnIdleResume; iTaskDefinition.Settings.IdleSettings.IdleDuration = ScheduledJobWTS.ConvertTimeSpanToWTSString(jobOptions.IdleDuration); iTaskDefinition.Settings.IdleSettings.WaitTimeout = ScheduledJobWTS.ConvertTimeSpanToWTSString(jobOptions.IdleTimeout); iTaskDefinition.Settings.Hidden = !jobOptions.ShowInTaskScheduler; iTaskDefinition.Settings.RunOnlyIfNetworkAvailable = !jobOptions.RunWithoutNetwork; iTaskDefinition.Settings.AllowDemandStart = !jobOptions.DoNotAllowDemandStart; iTaskDefinition.Settings.MultipleInstances = this.ConvertFromMultiInstances(jobOptions.MultipleInstancePolicy); TaskScheduler.IPrincipal principal = iTaskDefinition.Principal; if (jobOptions.RunElevated) { variable = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; } else { variable = _TASK_RUNLEVEL.TASK_RUNLEVEL_LUA; } principal.RunLevel = variable; }
private void AddTaskOptions(ITaskDefinition iTaskDefinition, ScheduledJobOptions jobOptions) { _TASK_RUNLEVEL variable; iTaskDefinition.Settings.DisallowStartIfOnBatteries = !jobOptions.StartIfOnBatteries; iTaskDefinition.Settings.StopIfGoingOnBatteries = jobOptions.StopIfGoingOnBatteries; iTaskDefinition.Settings.WakeToRun = jobOptions.WakeToRun; iTaskDefinition.Settings.RunOnlyIfIdle = !jobOptions.StartIfNotIdle; iTaskDefinition.Settings.IdleSettings.StopOnIdleEnd = jobOptions.StopIfGoingOffIdle; iTaskDefinition.Settings.IdleSettings.RestartOnIdle = jobOptions.RestartOnIdleResume; iTaskDefinition.Settings.IdleSettings.IdleDuration = ScheduledJobWTS.ConvertTimeSpanToWTSString(jobOptions.IdleDuration); iTaskDefinition.Settings.IdleSettings.WaitTimeout = ScheduledJobWTS.ConvertTimeSpanToWTSString(jobOptions.IdleTimeout); iTaskDefinition.Settings.Hidden = !jobOptions.ShowInTaskScheduler; iTaskDefinition.Settings.RunOnlyIfNetworkAvailable = !jobOptions.RunWithoutNetwork; iTaskDefinition.Settings.AllowDemandStart = !jobOptions.DoNotAllowDemandStart; iTaskDefinition.Settings.MultipleInstances = this.ConvertFromMultiInstances(jobOptions.MultipleInstancePolicy); TaskScheduler.IPrincipal principal = iTaskDefinition.Principal; if (jobOptions.RunElevated) { variable = _TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; } else { variable = _TASK_RUNLEVEL.TASK_RUNLEVEL_LUA; } principal.RunLevel = variable; }
private ScheduledJobDefinition(SerializationInfo info, StreamingContext context) { this._globalId = Guid.NewGuid(); this._name = string.Empty; this._id = ScheduledJobDefinition.GetCurrentId(); this._executionHistoryLength = ScheduledJobDefinition.DefaultExecutionHistoryLength; this._enabled = true; this._triggers = new Dictionary<int, ScheduledJobTrigger>(); if (info != null) { this._options = (ScheduledJobOptions)info.GetValue("Options_Member", typeof(ScheduledJobOptions)); this._globalId = Guid.Parse(info.GetString("GlobalId_Member")); this._name = info.GetString("Name_Member"); this._executionHistoryLength = info.GetInt32("HistoryLength_Member"); this._enabled = info.GetBoolean("Enabled_Member"); this._triggers = (Dictionary<int, ScheduledJobTrigger>)info.GetValue("Triggers_Member", typeof(Dictionary<int, ScheduledJobTrigger>)); this._currentTriggerId = info.GetInt32("CurrentTriggerId_Member"); this._definitionFilePath = info.GetString("FilePath_Member"); this._definitionOutputPath = info.GetString("OutputPath_Member"); object value = info.GetValue("InvocationInfo_Member", typeof(object)); this._invocationInfo = value as JobInvocationInfo; this._options.JobDefinition = this; foreach (ScheduledJobTrigger scheduledJobTrigger in this._triggers.Values) { scheduledJobTrigger.JobDefinition = this; } this._isDisposed = false; return; } else { throw new PSArgumentNullException("info"); } }
public ScheduledJobDefinition(JobInvocationInfo invocationInfo, IEnumerable<ScheduledJobTrigger> triggers, ScheduledJobOptions options, PSCredential credential) { ScheduledJobOptions scheduledJobOption; this._globalId = Guid.NewGuid(); this._name = string.Empty; this._id = ScheduledJobDefinition.GetCurrentId(); this._executionHistoryLength = ScheduledJobDefinition.DefaultExecutionHistoryLength; this._enabled = true; this._triggers = new Dictionary<int, ScheduledJobTrigger>(); if (invocationInfo != null) { this._name = invocationInfo.Name; this._invocationInfo = invocationInfo; this.SetTriggers(triggers, false); ScheduledJobDefinition scheduledJobDefinition = this; if (options != null) { scheduledJobOption = new ScheduledJobOptions(options); } else { scheduledJobOption = new ScheduledJobOptions(); } scheduledJobDefinition._options = scheduledJobOption; this._options.JobDefinition = this; this._credential = credential; return; } else { throw new PSArgumentNullException("invocationInfo"); } }
public void UpdateOptions(ScheduledJobOptions options, bool save) { ScheduledJobOptions scheduledJobOption; this.IsDisposed(); this._options.JobDefinition = null; ScheduledJobDefinition scheduledJobDefinition = this; if (options != null) { scheduledJobOption = new ScheduledJobOptions(options); } else { scheduledJobOption = new ScheduledJobOptions(); } scheduledJobDefinition._options = scheduledJobOption; this._options.JobDefinition = this; if (save) { this.Save(); } }
private bool UpdateDefintionFromWTS() { ScheduledJobTrigger current = null; ScheduledJobTrigger item = null; bool valueOrDefault = false; bool flag = false; using (ScheduledJobWTS scheduledJobWT = new ScheduledJobWTS()) { bool taskEnabled = scheduledJobWT.GetTaskEnabled(this._name); ScheduledJobOptions jobOptions = scheduledJobWT.GetJobOptions(this._name); Collection<ScheduledJobTrigger> jobTriggers = scheduledJobWT.GetJobTriggers(this._name); if (taskEnabled != this._enabled) { this._enabled = taskEnabled; flag = true; } if (jobOptions.DoNotAllowDemandStart != this._options.DoNotAllowDemandStart || jobOptions.IdleDuration != this._options.IdleDuration || jobOptions.IdleTimeout != this._options.IdleTimeout || jobOptions.MultipleInstancePolicy != this._options.MultipleInstancePolicy || jobOptions.RestartOnIdleResume != this._options.RestartOnIdleResume || jobOptions.RunElevated != this._options.RunElevated || jobOptions.RunWithoutNetwork != this._options.RunWithoutNetwork || jobOptions.ShowInTaskScheduler != this._options.ShowInTaskScheduler || jobOptions.StartIfNotIdle != this._options.StartIfNotIdle || jobOptions.StartIfOnBatteries != this._options.StartIfOnBatteries || jobOptions.StopIfGoingOffIdle != this._options.StopIfGoingOffIdle || jobOptions.StopIfGoingOnBatteries != this._options.StopIfGoingOnBatteries || jobOptions.WakeToRun != this._options.WakeToRun) { jobOptions.JobDefinition = this._options.JobDefinition; this._options = jobOptions; flag = true; } if (this._triggers.Count == jobTriggers.Count) { bool flag1 = false; IEnumerator<ScheduledJobTrigger> enumerator = jobTriggers.GetEnumerator(); using (enumerator) { do { if (!enumerator.MoveNext()) { continue; } current = enumerator.Current; if (this._triggers.ContainsKey(current.Id)) { item = this._triggers[current.Id]; if (item.DaysOfWeek != current.DaysOfWeek || item.Enabled != current.Enabled || item.Frequency != current.Frequency || item.Interval != current.Interval || item.RandomDelay != current.RandomDelay) { break; } DateTime? at = item.At; DateTime? nullable = current.At; if (at.HasValue != nullable.HasValue) { valueOrDefault = true; } else { if (!at.HasValue) { valueOrDefault = false; } else { valueOrDefault = at.GetValueOrDefault() != nullable.GetValueOrDefault(); } } } else { flag1 = true; break; } } while (!valueOrDefault && !(item.User != current.User)); flag1 = true; } if (flag1) { this.SetTriggers(jobTriggers, false); flag = true; } } else { this.SetTriggers(jobTriggers, false); flag = true; } } return flag; }
/// <summary> /// Compares the current ScheduledJobDefinition task scheduler information /// with the corresponding information stored in Task Scheduler. If the /// information is different then the task scheduler information in this /// object is updated to match what is in Task Scheduler, since that information /// takes precedence. /// /// Task Scheduler information: /// - Triggers /// - Options /// - Enabled state /// </summary> /// <returns>Boolean if this object data is modified.</returns> private bool UpdateDefintionFromWTS() { bool dataModified = false; // Get information from Task Scheduler. using (ScheduledJobWTS taskScheduler = new ScheduledJobWTS()) { bool wtsEnabled = taskScheduler.GetTaskEnabled(_name); ScheduledJobOptions wtsOptions = taskScheduler.GetJobOptions(_name); Collection<ScheduledJobTrigger> wtsTriggers = taskScheduler.GetJobTriggers(_name); // // Compare with existing object data and modify if necessary. // // Enabled. if (wtsEnabled != _enabled) { _enabled = wtsEnabled; dataModified = true; } // Options. if (wtsOptions.DoNotAllowDemandStart != _options.DoNotAllowDemandStart || wtsOptions.IdleDuration != _options.IdleDuration || wtsOptions.IdleTimeout != _options.IdleTimeout || wtsOptions.MultipleInstancePolicy != _options.MultipleInstancePolicy || wtsOptions.RestartOnIdleResume != _options.RestartOnIdleResume || wtsOptions.RunElevated != _options.RunElevated || wtsOptions.RunWithoutNetwork != _options.RunWithoutNetwork || wtsOptions.ShowInTaskScheduler != _options.ShowInTaskScheduler || wtsOptions.StartIfNotIdle != _options.StartIfNotIdle || wtsOptions.StartIfOnBatteries != _options.StartIfOnBatteries || wtsOptions.StopIfGoingOffIdle != _options.StopIfGoingOffIdle || wtsOptions.StopIfGoingOnBatteries != _options.StopIfGoingOnBatteries || wtsOptions.WakeToRun != _options.WakeToRun) { // Keep the current scheduled job definition reference. wtsOptions.JobDefinition = _options.JobDefinition; _options = wtsOptions; dataModified = true; } // Triggers. if (_triggers.Count != wtsTriggers.Count) { SetTriggers(wtsTriggers, false); dataModified = true; } else { bool foundTriggerDiff = false; // Compare each trigger object. foreach (var wtsTrigger in wtsTriggers) { if (_triggers.ContainsKey(wtsTrigger.Id) == false) { foundTriggerDiff = true; break; } ScheduledJobTrigger trigger = _triggers[wtsTrigger.Id]; if (trigger.DaysOfWeek != wtsTrigger.DaysOfWeek || trigger.Enabled != wtsTrigger.Enabled || trigger.Frequency != wtsTrigger.Frequency || trigger.Interval != wtsTrigger.Interval || trigger.RandomDelay != wtsTrigger.RandomDelay || trigger.At != wtsTrigger.At || trigger.User != wtsTrigger.User) { foundTriggerDiff = true; break; } } if (foundTriggerDiff) { SetTriggers(wtsTriggers, false); dataModified = true; } } } return dataModified; }
private ScheduledJobDefinition( SerializationInfo info, StreamingContext context) { if (info == null) { throw new PSArgumentNullException("info"); } _options = (ScheduledJobOptions)info.GetValue("Options_Member", typeof(ScheduledJobOptions)); _globalId = Guid.Parse(info.GetString("GlobalId_Member")); _name = info.GetString("Name_Member"); _executionHistoryLength = info.GetInt32("HistoryLength_Member"); _enabled = info.GetBoolean("Enabled_Member"); _triggers = (Dictionary<Int32, ScheduledJobTrigger>)info.GetValue("Triggers_Member", typeof(Dictionary<Int32, ScheduledJobTrigger>)); _currentTriggerId = info.GetInt32("CurrentTriggerId_Member"); _definitionFilePath = info.GetString("FilePath_Member"); _definitionOutputPath = info.GetString("OutputPath_Member"); object invocationObject = info.GetValue("InvocationInfo_Member", typeof(object)); _invocationInfo = invocationObject as JobInvocationInfo; // Set the JobDefinition reference for the ScheduledJobTrigger and // ScheduledJobOptions objects. _options.JobDefinition = this; foreach (ScheduledJobTrigger trigger in _triggers.Values) { trigger.JobDefinition = this; } // Instance information. _isDisposed = false; }
/// <summary> /// Constructor. /// </summary> /// <param name="invocationInfo">Information to invoke Job</param> /// <param name="triggers">ScheduledJobTriggers</param> /// <param name="options">ScheduledJobOptions</param> /// <param name="credential">Credential</param> public ScheduledJobDefinition( JobInvocationInfo invocationInfo, IEnumerable<ScheduledJobTrigger> triggers, ScheduledJobOptions options, PSCredential credential) { if (invocationInfo == null) { throw new PSArgumentNullException("invocationInfo"); } _name = invocationInfo.Name; _invocationInfo = invocationInfo; SetTriggers(triggers, false); _options = (options != null) ? new ScheduledJobOptions(options) : new ScheduledJobOptions(); _options.JobDefinition = this; _credential = credential; }
/// <summary> /// Updates scheduled job options. /// </summary> /// <param name="options">ScheduledJobOptions or null for default</param> /// <param name="save">Update Windows Task Scheduler and save to store</param> public void UpdateOptions( ScheduledJobOptions options, bool save) { IsDisposed(); // Disassociate current options object from this definition. _options.JobDefinition = null; // options == null is allowed and signals the use default // Task Scheduler options. _options = (options != null) ? new ScheduledJobOptions(options) : new ScheduledJobOptions(); _options.JobDefinition = this; if (save) { Save(); } }
/// <summary> /// Copy Constructor. /// </summary> /// <param name="copyOptions">Copy from</param> internal ScheduledJobOptions( ScheduledJobOptions copyOptions) { if (copyOptions == null) { throw new PSArgumentNullException("copyOptions"); } _startIfOnBatteries = copyOptions.StartIfOnBatteries; _stopIfGoingOnBatteries = copyOptions.StopIfGoingOnBatteries; _wakeToRun = copyOptions.WakeToRun; _startIfNotIdle = copyOptions.StartIfNotIdle; _stopIfGoingOffIdle = copyOptions.StopIfGoingOffIdle; _restartOnIdleResume = copyOptions.RestartOnIdleResume; _idleDuration = copyOptions.IdleDuration; _idleTimeout = copyOptions.IdleTimeout; _showInTaskScheduler = copyOptions.ShowInTaskScheduler; _runElevated = copyOptions.RunElevated; _runWithoutNetwork = copyOptions.RunWithoutNetwork; _donotAllowDemandStart = copyOptions.DoNotAllowDemandStart; _multipleInstancePolicy = copyOptions.MultipleInstancePolicy; _jobDefAssociation = copyOptions.JobDefinition; }