lib/win32/taskscheduler.rb in win32-taskscheduler-2.0.1 vs lib/win32/taskscheduler.rb in win32-taskscheduler-2.0.4
- old
+ new
@@ -1,18 +1,17 @@
-require_relative 'taskscheduler/sid'
-require_relative 'taskscheduler/helper'
-require_relative 'taskscheduler/time_calc_helper'
-require_relative 'taskscheduler/constants'
-require_relative 'taskscheduler/version'
-require 'win32ole'
-require 'socket'
-require 'time'
-require 'structured_warnings'
+require_relative "taskscheduler/sid"
+require_relative "taskscheduler/helper"
+require_relative "taskscheduler/time_calc_helper"
+require_relative "taskscheduler/constants"
+require_relative "taskscheduler/version"
+require "win32ole"
+require "socket"
+require "time"
+require "structured_warnings"
# The Win32 module serves as a namespace only
module Win32
-
# The TaskScheduler class encapsulates a Windows scheduled task
class TaskScheduler
include Win32::TaskScheduler::Helper
include Win32::TaskScheduler::TaskSchedulerConstants
include Win32::TaskScheduler::TimeCalcHelper
@@ -115,17 +114,15 @@
TWENTY_NINTH = TASK_TWENTY_NINTH
THIRTYETH = TASK_THIRTYETH
THIRTY_FIRST = TASK_THIRTY_FIRST
LAST = TASK_LAST
-
# :startdoc:
attr_accessor :password
attr_reader :host
-
def root_path(path = '\\')
path
end
# Returns a new TaskScheduler object, attached to +folder+. If that
@@ -142,18 +139,18 @@
@host = Socket.gethostname
@task = nil
@password = nil
- raise ArgumentError, "invalid folder" unless folder.include?("\\")
+ raise ArgumentError, "invalid folder" unless folder.include?('\\')
unless [TrueClass, FalseClass].include?(force.class)
raise TypeError, "invalid force value"
end
begin
- @service = WIN32OLE.new('Schedule.Service')
+ @service = WIN32OLE.new("Schedule.Service")
rescue WIN32OLERuntimeError => err
raise Error, err.inspect
end
@service.Connect
@@ -171,13 +168,11 @@
end
else
@root = @service.GetFolder(folder)
end
- if task && trigger
- new_work_item(task, trigger)
- end
+ new_work_item(task, trigger) if task && trigger
end
# Returns an array of scheduled task names.
#
def enum
@@ -199,17 +194,17 @@
#
def exists?(full_task_path)
path = nil
task_name = nil
- if full_task_path.include?("\\")
+ if full_task_path.include?('\\')
*path, task_name = full_task_path.split('\\')
else
task_name = full_task_path
end
- folder = path.nil? ? root_path : path.join("\\")
+ folder = path.nil? ? root_path : path.join('\\')
begin
root = @service.GetFolder(folder)
rescue WIN32OLERuntimeError => err
return false
@@ -234,11 +229,11 @@
begin
registeredTask = @root.GetTask(task)
@task = registeredTask
rescue WIN32OLERuntimeError => err
- raise Error, ole_error('activate', err)
+ raise Error, ole_error("activate", err)
end
end
# Activate the specified task.
#
@@ -248,11 +243,11 @@
begin
registeredTask = @root.GetTask(task)
registeredTask.Enabled = 1
@task = registeredTask
rescue WIN32OLERuntimeError => err
- raise Error, ole_error('activate', err)
+ raise Error, ole_error("activate", err)
end
end
# Delete the specified task name.
#
@@ -260,11 +255,11 @@
raise TypeError unless task.is_a?(String)
begin
@root.DeleteTask(task, 0)
rescue WIN32OLERuntimeError => err
- raise Error, ole_error('DeleteTask', err)
+ raise Error, ole_error("DeleteTask", err)
end
end
# Execute the current task.
#
@@ -274,11 +269,11 @@
end
# This method no longer has any effect. It is a no-op that remains for
# backwards compatibility. It will be removed in 0.4.0.
#
- def save(file = nil)
+ def save(_file = nil)
warn DeprecatedMethodWarning, "this method is no longer necessary"
check_for_active_task
# Do nothing, deprecated.
end
@@ -298,11 +293,11 @@
raise TypeError unless host.is_a?(String)
begin
@service.Connect(host)
rescue WIN32OLERuntimeError => err
- raise Error, ole_error('Connect', err)
+ raise Error, ole_error("Connect", err)
end
@host = host
host
end
@@ -315,11 +310,11 @@
raise TypeError unless host.is_a?(String)
begin
@service.Connect(host, user, domain, password)
rescue WIN32OLERuntimeError => err
- raise Error, ole_error('Connect', err)
+ raise Error, ole_error("Connect", err)
end
@host = host
host
end
@@ -329,11 +324,12 @@
alias set_host set_machine
# Sets the +user+ and +password+ for the given task. If the user and
# password are set properly then true is returned.
# throws TypeError if password is not provided for other than system users
- def set_account_information(user_id, password)
+ def set_account_information(user_id, password, interactive)
+ @interactive ||= interactive
check_credential_requirements(user_id, password)
check_for_active_task
@password = password
register_task_definition(@task.Definition, @task.Path, user_id, password)
true
@@ -461,36 +457,36 @@
# 'above_normal_3', 'above_normal_2', 'highest', 'critical' and 'unknown'.
#
def priority
check_for_active_task
- case @task.Definition.Settings.Priority
- when 0
- priority = 'critical'
- when 1
- priority = 'highest'
- when 2
- priority = 'above_normal_2'
- when 3
- priority = 'above_normal_3'
- when 4
- priority = 'normal_4'
- when 5
- priority = 'normal_5'
- when 6
- priority = 'normal_6'
- when 7
- priority = 'below_normal_7'
- when 8
- priority = 'below_normal_8'
- when 9
- priority = 'lowest'
- when 10
- priority = 'idle'
- else
- priority = 'unknown'
- end
+ priority = case @task.Definition.Settings.Priority
+ when 0
+ "critical"
+ when 1
+ "highest"
+ when 2
+ "above_normal_2"
+ when 3
+ "above_normal_3"
+ when 4
+ "normal_4"
+ when 5
+ "normal_5"
+ when 6
+ "normal_6"
+ when 7
+ "below_normal_7"
+ when 8
+ "below_normal_8"
+ when 9
+ "lowest"
+ when 10
+ "idle"
+ else
+ "unknown"
+ end
priority
end
# Sets the priority of the task. The +priority+ should be a numeric
@@ -515,104 +511,96 @@
def new_work_item(task, trigger, userinfo = { user: nil, password: nil, interactive: false })
raise TypeError unless userinfo.is_a?(Hash) && task.is_a?(String) && trigger.is_a?(Hash)
# If user ID is not given, consider it as a 'SYSTEM' user
userinfo[:user] = SERVICE_ACCOUNT_USERS.first if userinfo[:user].to_s.empty?
+ @password = userinfo[:password]
+ @interactive = userinfo[:interactive]
check_credential_requirements(userinfo[:user], userinfo[:password])
taskDefinition = @service.NewTask(0)
- taskDefinition.RegistrationInfo.Description = ''
- taskDefinition.RegistrationInfo.Author = ''
+ taskDefinition.RegistrationInfo.Description = ""
+ taskDefinition.RegistrationInfo.Author = ""
taskDefinition.Settings.StartWhenAvailable = false
- taskDefinition.Settings.Enabled = true
+ taskDefinition.Settings.Enabled = true
taskDefinition.Settings.Hidden = false
-
-
unless trigger.empty?
- raise ArgumentError, 'Unknown trigger type' unless valid_trigger_option(trigger[:trigger_type])
+ raise ArgumentError, "Unknown trigger type" unless valid_trigger_option(trigger[:trigger_type])
validate_trigger(trigger)
- startTime = "%04d-%02d-%02dT%02d:%02d:00" % [
- trigger[:start_year], trigger[:start_month], trigger[:start_day],
- trigger[:start_hour], trigger[:start_minute]
- ]
+ startTime = format("%04d-%02d-%02dT%02d:%02d:00", trigger[:start_year], trigger[:start_month], trigger[:start_day], trigger[:start_hour], trigger[:start_minute])
# Set defaults
trigger[:end_year] ||= 0
trigger[:end_month] ||= 0
trigger[:end_day] ||= 0
- endTime = "%04d-%02d-%02dT00:00:00" % [
- trigger[:end_year], trigger[:end_month], trigger[:end_day]
- ]
+ endTime = format("%04d-%02d-%02dT00:00:00", trigger[:end_year], trigger[:end_month], trigger[:end_day])
trig = taskDefinition.Triggers.Create(trigger[:trigger_type].to_i)
trig.Id = "RegistrationTriggerId#{taskDefinition.Triggers.Count}"
- trig.StartBoundary = startTime if startTime != '0000-00-00T00:00:00'
- trig.EndBoundary = endTime if endTime != '0000-00-00T00:00:00'
+ trig.StartBoundary = startTime if startTime != "0000-00-00T00:00:00"
+ trig.EndBoundary = endTime if endTime != "0000-00-00T00:00:00"
trig.Enabled = true
repetitionPattern = trig.Repetition
if trigger[:minutes_duration].to_i > 0
- repetitionPattern.Duration = "PT#{trigger[:minutes_duration]||0}M"
+ repetitionPattern.Duration = "PT#{trigger[:minutes_duration] || 0}M"
end
if trigger[:minutes_interval].to_i > 0
- repetitionPattern.Interval = "PT#{trigger[:minutes_interval]||0}M"
+ repetitionPattern.Interval = "PT#{trigger[:minutes_interval] || 0}M"
end
tmp = trigger[:type]
tmp = nil unless tmp.is_a?(Hash)
case trigger[:trigger_type]
- when TASK_TIME_TRIGGER_DAILY
- trig.DaysInterval = tmp[:days_interval] if tmp && tmp[:days_interval]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]}M"
- end
- when TASK_TIME_TRIGGER_WEEKLY
- trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
- trig.WeeksInterval = tmp[:weeks_interval] if tmp && tmp[:weeks_interval]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- when TASK_TIME_TRIGGER_MONTHLYDATE
- trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
- trig.DaysOfMonth = tmp[:days] if tmp && tmp[:days]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- trig.RunOnLastDayOfMonth = trigger[:run_on_last_day_of_month] if trigger[:run_on_last_day_of_month]
- when TASK_TIME_TRIGGER_MONTHLYDOW
- trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
- trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
- trig.WeeksOfMonth = tmp[:weeks_of_month] if tmp && tmp[:weeks_of_month]
- if trigger[:random_minutes_interval].to_i>0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- trig.RunOnLastWeekOfMonth = trigger[:run_on_last_week_of_month] if trigger[:run_on_last_week_of_month]
- when TASK_TIME_TRIGGER_ONCE
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- when TASK_EVENT_TRIGGER_AT_SYSTEMSTART
- trig.Delay = "PT#{trigger[:delay_duration]||0}M"
- when TASK_EVENT_TRIGGER_AT_LOGON
- trig.UserId = trigger[:user_id] if trigger[:user_id]
- trig.Delay = "PT#{trigger[:delay_duration]||0}M"
+ when TASK_TIME_TRIGGER_DAILY
+ trig.DaysInterval = tmp[:days_interval] if tmp && tmp[:days_interval]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval]}M"
+ end
+ when TASK_TIME_TRIGGER_WEEKLY
+ trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
+ trig.WeeksInterval = tmp[:weeks_interval] if tmp && tmp[:weeks_interval]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ when TASK_TIME_TRIGGER_MONTHLYDATE
+ trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
+ trig.DaysOfMonth = tmp[:days] if tmp && tmp[:days]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ trig.RunOnLastDayOfMonth = trigger[:run_on_last_day_of_month] if trigger[:run_on_last_day_of_month]
+ when TASK_TIME_TRIGGER_MONTHLYDOW
+ trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
+ trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
+ trig.WeeksOfMonth = tmp[:weeks_of_month] if tmp && tmp[:weeks_of_month]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ trig.RunOnLastWeekOfMonth = trigger[:run_on_last_week_of_month] if trigger[:run_on_last_week_of_month]
+ when TASK_TIME_TRIGGER_ONCE
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ when TASK_EVENT_TRIGGER_AT_SYSTEMSTART
+ trig.Delay = "PT#{trigger[:delay_duration] || 0}M"
+ when TASK_EVENT_TRIGGER_AT_LOGON
+ trig.UserId = trigger[:user_id] if trigger[:user_id]
+ trig.Delay = "PT#{trigger[:delay_duration] || 0}M"
end
end
act = taskDefinition.Actions.Create(0)
- act.Path = 'cmd'
+ act.Path = "cmd"
- @password = userinfo[:password]
- @interactive = userinfo[:interactive]
-
register_task_definition(taskDefinition, task, userinfo[:user], userinfo[:password])
@task = @root.GetTask(task)
end
@@ -632,11 +620,11 @@
# Example: "At 7:14 AM every day, starting 4/11/2015"
#
def trigger_string(index)
raise TypeError unless index.is_a?(Numeric)
check_for_active_task
- index += 1 # first item index is 1
+ index += 1 # first item index is 1
begin
trigger = @task.Definition.Triggers.Item(index)
rescue WIN32OLERuntimeError
raise Error, "No trigger found at index '#{index}'"
@@ -650,11 +638,11 @@
# TODO: Fix.
#
def delete_trigger(index)
raise TypeError unless index.is_a?(Numeric)
check_for_active_task
- index += 1 # first item index is 1
+ index += 1 # first item index is 1
definition = @task.Definition
definition.Triggers.Remove(index)
register_task_definition(definition)
@@ -665,61 +653,61 @@
# current task.
#
def trigger(index)
raise TypeError unless index.is_a?(Numeric)
check_for_active_task
- index += 1 # first item index is 1
+ index += 1 # first item index is 1
begin
trig = @task.Definition.Triggers.Item(index)
rescue WIN32OLERuntimeError => err
- raise Error, ole_error('Item', err)
+ raise Error, ole_error("Item", err)
end
trigger = {}
case trig.Type
- when TASK_TIME_TRIGGER_DAILY
- tmp = {}
- tmp[:days_interval] = trig.DaysInterval
- trigger[:type] = tmp
- trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
- when TASK_TIME_TRIGGER_WEEKLY
- tmp = {}
- tmp[:weeks_interval] = trig.WeeksInterval
- tmp[:days_of_week] = trig.DaysOfWeek
- trigger[:type] = tmp
- trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
- when TASK_TIME_TRIGGER_MONTHLYDATE
- tmp = {}
- tmp[:months] = trig.MonthsOfYear
- tmp[:days] = trig.DaysOfMonth
- trigger[:type] = tmp
- trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
- trigger[:run_on_last_day_of_month] = trig.RunOnLastDayOfMonth
- when TASK_TIME_TRIGGER_MONTHLYDOW
- tmp = {}
- tmp[:months] = trig.MonthsOfYear
- tmp[:days_of_week] = trig.DaysOfWeek
- tmp[:weeks_of_month] = trig.WeeksOfMonth
- trigger[:type] = tmp
- trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
- trigger[:run_on_last_week_of_month] = trig.RunOnLastWeekOfMonth
- when TASK_TIME_TRIGGER_ONCE
- tmp = {}
- tmp[:once] = nil
- trigger[:type] = tmp
- trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
- when TASK_EVENT_TRIGGER_AT_SYSTEMSTART
- trigger[:delay_duration] = time_in_minutes(trig.Delay)
- when TASK_EVENT_TRIGGER_AT_LOGON
- trigger[:user_id] = trig.UserId if trig.UserId.to_s != ""
- trigger[:delay_duration] = time_in_minutes(trig.Delay)
- when TASK_EVENT_TRIGGER_ON_IDLE
- trigger[:execution_time_limit] = time_in_minutes(trig.ExecutionTimeLimit)
- else
- raise Error, 'Unknown trigger type'
+ when TASK_TIME_TRIGGER_DAILY
+ tmp = {}
+ tmp[:days_interval] = trig.DaysInterval
+ trigger[:type] = tmp
+ trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
+ when TASK_TIME_TRIGGER_WEEKLY
+ tmp = {}
+ tmp[:weeks_interval] = trig.WeeksInterval
+ tmp[:days_of_week] = trig.DaysOfWeek
+ trigger[:type] = tmp
+ trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
+ when TASK_TIME_TRIGGER_MONTHLYDATE
+ tmp = {}
+ tmp[:months] = trig.MonthsOfYear
+ tmp[:days] = trig.DaysOfMonth
+ trigger[:type] = tmp
+ trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
+ trigger[:run_on_last_day_of_month] = trig.RunOnLastDayOfMonth
+ when TASK_TIME_TRIGGER_MONTHLYDOW
+ tmp = {}
+ tmp[:months] = trig.MonthsOfYear
+ tmp[:days_of_week] = trig.DaysOfWeek
+ tmp[:weeks_of_month] = trig.WeeksOfMonth
+ trigger[:type] = tmp
+ trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
+ trigger[:run_on_last_week_of_month] = trig.RunOnLastWeekOfMonth
+ when TASK_TIME_TRIGGER_ONCE
+ tmp = {}
+ tmp[:once] = nil
+ trigger[:type] = tmp
+ trigger[:random_minutes_interval] = time_in_minutes(trig.RandomDelay)
+ when TASK_EVENT_TRIGGER_AT_SYSTEMSTART
+ trigger[:delay_duration] = time_in_minutes(trig.Delay)
+ when TASK_EVENT_TRIGGER_AT_LOGON
+ trigger[:user_id] = trig.UserId if trig.UserId.to_s != ""
+ trigger[:delay_duration] = time_in_minutes(trig.Delay)
+ when TASK_EVENT_TRIGGER_ON_IDLE
+ trigger[:execution_time_limit] = time_in_minutes(trig.ExecutionTimeLimit)
+ else
+ raise Error, "Unknown trigger type"
end
trigger[:start_year], trigger[:start_month], trigger[:start_day],
trigger[:start_hour], trigger[:start_minute] = trig.StartBoundary.scan(/(\d+)-(\d+)-(\d+)T(\d+):(\d+)/).first
@@ -757,88 +745,83 @@
# * weeks
# * weeks_interval
#
def trigger=(trigger)
raise TypeError unless trigger.is_a?(Hash)
- raise ArgumentError, 'Unknown trigger type' unless valid_trigger_option(trigger[:trigger_type])
+ raise ArgumentError, "Unknown trigger type" unless valid_trigger_option(trigger[:trigger_type])
check_for_active_task
validate_trigger(trigger)
definition = @task.Definition
definition.Triggers.Clear()
- startTime = "%04d-%02d-%02dT%02d:%02d:00" % [
- trigger[:start_year], trigger[:start_month],
- trigger[:start_day], trigger[:start_hour], trigger[:start_minute]
- ]
+ startTime = format("%04d-%02d-%02dT%02d:%02d:00", trigger[:start_year], trigger[:start_month], trigger[:start_day], trigger[:start_hour], trigger[:start_minute])
- endTime = "%04d-%02d-%02dT00:00:00" % [
- trigger[:end_year], trigger[:end_month], trigger[:end_day]
- ]
+ endTime = format("%04d-%02d-%02dT00:00:00", trigger[:end_year], trigger[:end_month], trigger[:end_day])
- trig = definition.Triggers.Create( trigger[:trigger_type].to_i )
+ trig = definition.Triggers.Create(trigger[:trigger_type].to_i)
trig.Id = "RegistrationTriggerId#{definition.Triggers.Count}"
- trig.StartBoundary = startTime if startTime != '0000-00-00T00:00:00'
- trig.EndBoundary = endTime if endTime != '0000-00-00T00:00:00'
+ trig.StartBoundary = startTime if startTime != "0000-00-00T00:00:00"
+ trig.EndBoundary = endTime if endTime != "0000-00-00T00:00:00"
trig.Enabled = true
repetitionPattern = trig.Repetition
if trigger[:minutes_duration].to_i > 0
- repetitionPattern.Duration = "PT#{trigger[:minutes_duration]||0}M"
+ repetitionPattern.Duration = "PT#{trigger[:minutes_duration] || 0}M"
end
if trigger[:minutes_interval].to_i > 0
- repetitionPattern.Interval = "PT#{trigger[:minutes_interval]||0}M"
+ repetitionPattern.Interval = "PT#{trigger[:minutes_interval] || 0}M"
end
tmp = trigger[:type]
tmp = nil unless tmp.is_a?(Hash)
case trigger[:trigger_type]
- when TASK_TIME_TRIGGER_DAILY
- trig.DaysInterval = tmp[:days_interval] if tmp && tmp[:days_interval]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]}M"
- end
- when TASK_TIME_TRIGGER_WEEKLY
- trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
- trig.WeeksInterval = tmp[:weeks_interval] if tmp && tmp[:weeks_interval]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- when TASK_TIME_TRIGGER_MONTHLYDATE
- trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
- trig.DaysOfMonth = tmp[:days] if tmp && tmp[:days]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- trig.RunOnLastDayOfMonth = trigger[:run_on_last_day_of_month] if trigger[:run_on_last_day_of_month]
- when TASK_TIME_TRIGGER_MONTHLYDOW
- trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
- trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
- trig.WeeksOfMonth = tmp[:weeks_of_month] if tmp && tmp[:weeks_of_month]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- trig.RunOnLastWeekOfMonth = trigger[:run_on_last_week_of_month] if trigger[:run_on_last_week_of_month]
- when TASK_TIME_TRIGGER_ONCE
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- when TASK_EVENT_TRIGGER_AT_SYSTEMSTART
- trig.Delay = "PT#{trigger[:delay_duration]||0}M"
- when TASK_EVENT_TRIGGER_AT_LOGON
- trig.UserId = trigger[:user_id] if trigger[:user_id]
- trig.Delay = "PT#{trigger[:delay_duration]||0}M"
- when TASK_EVENT_TRIGGER_ON_IDLE
- # for setting execution time limit Ref : https://msdn.microsoft.com/en-us/library/windows/desktop/aa380724(v=vs.85).aspx
- if trigger[:execution_time_limit].to_i > 0
- trig.ExecutionTimeLimit = "PT#{trigger[:execution_time_limit]||0}M"
- end
+ when TASK_TIME_TRIGGER_DAILY
+ trig.DaysInterval = tmp[:days_interval] if tmp && tmp[:days_interval]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval]}M"
+ end
+ when TASK_TIME_TRIGGER_WEEKLY
+ trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
+ trig.WeeksInterval = tmp[:weeks_interval] if tmp && tmp[:weeks_interval]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ when TASK_TIME_TRIGGER_MONTHLYDATE
+ trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
+ trig.DaysOfMonth = tmp[:days] if tmp && tmp[:days]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ trig.RunOnLastDayOfMonth = trigger[:run_on_last_day_of_month] if trigger[:run_on_last_day_of_month]
+ when TASK_TIME_TRIGGER_MONTHLYDOW
+ trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
+ trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
+ trig.WeeksOfMonth = tmp[:weeks_of_month] if tmp && tmp[:weeks_of_month]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ trig.RunOnLastWeekOfMonth = trigger[:run_on_last_week_of_month] if trigger[:run_on_last_week_of_month]
+ when TASK_TIME_TRIGGER_ONCE
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ when TASK_EVENT_TRIGGER_AT_SYSTEMSTART
+ trig.Delay = "PT#{trigger[:delay_duration] || 0}M"
+ when TASK_EVENT_TRIGGER_AT_LOGON
+ trig.UserId = trigger[:user_id] if trigger[:user_id]
+ trig.Delay = "PT#{trigger[:delay_duration] || 0}M"
+ when TASK_EVENT_TRIGGER_ON_IDLE
+ # for setting execution time limit Ref : https://msdn.microsoft.com/en-us/library/windows/desktop/aa380724(v=vs.85).aspx
+ if trigger[:execution_time_limit].to_i > 0
+ trig.ExecutionTimeLimit = "PT#{trigger[:execution_time_limit] || 0}M"
+ end
end
register_task_definition(definition)
trigger
@@ -847,85 +830,80 @@
# Adds a trigger at the specified index.
#
def add_trigger(index, trigger)
raise TypeError unless index.is_a?(Numeric)
raise TypeError unless trigger.is_a?(Hash)
- raise ArgumentError, 'Unknown trigger type' unless valid_trigger_option(trigger[:trigger_type])
+ raise ArgumentError, "Unknown trigger type" unless valid_trigger_option(trigger[:trigger_type])
check_for_active_task
definition = @task.Definition
- startTime = "%04d-%02d-%02dT%02d:%02d:00" % [
- trigger[:start_year], trigger[:start_month], trigger[:start_day],
- trigger[:start_hour], trigger[:start_minute]
- ]
+ startTime = format("%04d-%02d-%02dT%02d:%02d:00", trigger[:start_year], trigger[:start_month], trigger[:start_day], trigger[:start_hour], trigger[:start_minute])
# Set defaults
trigger[:end_year] ||= 0
trigger[:end_month] ||= 0
trigger[:end_day] ||= 0
- endTime = "%04d-%02d-%02dT00:00:00" % [
- trigger[:end_year], trigger[:end_month], trigger[:end_day]
- ]
+ endTime = format("%04d-%02d-%02dT00:00:00", trigger[:end_year], trigger[:end_month], trigger[:end_day])
- trig = definition.Triggers.Create( trigger[:trigger_type].to_i )
+ trig = definition.Triggers.Create(trigger[:trigger_type].to_i)
trig.Id = "RegistrationTriggerId#{definition.Triggers.Count}"
- trig.StartBoundary = startTime if startTime != '0000-00-00T00:00:00'
- trig.EndBoundary = endTime if endTime != '0000-00-00T00:00:00'
+ trig.StartBoundary = startTime if startTime != "0000-00-00T00:00:00"
+ trig.EndBoundary = endTime if endTime != "0000-00-00T00:00:00"
trig.Enabled = true
repetitionPattern = trig.Repetition
if trigger[:minutes_duration].to_i > 0
- repetitionPattern.Duration = "PT#{trigger[:minutes_duration]||0}M"
+ repetitionPattern.Duration = "PT#{trigger[:minutes_duration] || 0}M"
end
if trigger[:minutes_interval].to_i > 0
- repetitionPattern.Interval = "PT#{trigger[:minutes_interval]||0}M"
+ repetitionPattern.Interval = "PT#{trigger[:minutes_interval] || 0}M"
end
tmp = trigger[:type]
tmp = nil unless tmp.is_a?(Hash)
case trigger[:trigger_type]
- when TASK_TIME_TRIGGER_DAILY
- trig.DaysInterval = tmp[:days_interval] if tmp && tmp[:days_interval]
- if trigger[:random_minutes_interval].to_i > 0
+ when TASK_TIME_TRIGGER_DAILY
+ trig.DaysInterval = tmp[:days_interval] if tmp && tmp[:days_interval]
+ if trigger[:random_minutes_interval].to_i > 0
trig.RandomDelay = "PT#{trigger[:random_minutes_interval]}M"
- end
- when TASK_TIME_TRIGGER_WEEKLY
- trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
- trig.WeeksInterval = tmp[:weeks_interval] if tmp && tmp[:weeks_interval]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- when TASK_TIME_TRIGGER_MONTHLYDATE
- trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
- trig.DaysOfMonth = tmp[:days] if tmp && tmp[:days]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- trig.RunOnLastDayOfMonth = trigger[:run_on_last_day_of_month] if trigger[:run_on_last_day_of_month]
- when TASK_TIME_TRIGGER_MONTHLYDOW
- trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
- trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
- trig.WeeksOfMonth = tmp[:weeks_of_month] if tmp && tmp[:weeks_of_month]
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- trig.RunOnLastWeekOfMonth = trigger[:run_on_last_week_of_month] if trigger[:run_on_last_week_of_month]
- when TASK_TIME_TRIGGER_ONCE
- if trigger[:random_minutes_interval].to_i > 0
- trig.RandomDelay = "PT#{trigger[:random_minutes_interval]||0}M"
- end
- when TASK_EVENT_TRIGGER_AT_SYSTEMSTART
- trig.Delay = "PT#{trigger[:delay_duration]||0}M"
- when TASK_EVENT_TRIGGER_AT_LOGON
- trig.UserId = trigger[:user_id] if trigger[:user_id]
- trig.Delay = "PT#{trigger[:delay_duration]||0}M"
+ end
+ when TASK_TIME_TRIGGER_WEEKLY
+ trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
+ trig.WeeksInterval = tmp[:weeks_interval] if tmp && tmp[:weeks_interval]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ when TASK_TIME_TRIGGER_MONTHLYDATE
+ trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
+ trig.DaysOfMonth = tmp[:days] if tmp && tmp[:days]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ trig.RunOnLastDayOfMonth = trigger[:run_on_last_day_of_month] if trigger[:run_on_last_day_of_month]
+ when TASK_TIME_TRIGGER_MONTHLYDOW
+ trig.MonthsOfYear = tmp[:months] if tmp && tmp[:months]
+ trig.DaysOfWeek = tmp[:days_of_week] if tmp && tmp[:days_of_week]
+ trig.WeeksOfMonth = tmp[:weeks_of_month] if tmp && tmp[:weeks_of_month]
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ trig.RunOnLastWeekOfMonth = trigger[:run_on_last_week_of_month] if trigger[:run_on_last_week_of_month]
+ when TASK_TIME_TRIGGER_ONCE
+ if trigger[:random_minutes_interval].to_i > 0
+ trig.RandomDelay = "PT#{trigger[:random_minutes_interval] || 0}M"
+ end
+ when TASK_EVENT_TRIGGER_AT_SYSTEMSTART
+ trig.Delay = "PT#{trigger[:delay_duration] || 0}M"
+ when TASK_EVENT_TRIGGER_AT_LOGON
+ trig.UserId = trigger[:user_id] if trigger[:user_id]
+ trig.Delay = "PT#{trigger[:delay_duration] || 0}M"
end
register_task_definition(definition)
true
@@ -935,22 +913,22 @@
# 'ready', 'running', 'not scheduled' or 'unknown'.
#
def status
check_for_active_task
- case @task.State
- when 3
- status = 'ready'
- when 4
- status = 'running'
- when 2
- status = 'queued'
- when 1
- status = 'not scheduled'
- else
- status = 'unknown'
- end
+ status = case @task.State
+ when 3
+ "ready"
+ when 4
+ "running"
+ when 2
+ "queued"
+ when 1
+ "not scheduled"
+ else
+ "unknown"
+ end
status
end
# Returns true if current task is enabled
@@ -1029,11 +1007,11 @@
time = nil
begin
time = Time.parse(@task.LastRunTime)
- rescue
+ rescue StandardError
# Ignore
end
time
end
@@ -1081,11 +1059,11 @@
raise TypeError unless max_run_time.is_a?(Numeric)
check_for_active_task
t = max_run_time
t /= 1000
- limit ="PT#{t}S"
+ limit = "PT#{t}S"
definition = @task.Definition
definition.Settings.ExecutionTimeLimit = limit
register_task_definition(definition)
@@ -1094,11 +1072,11 @@
# The Idle settings of a task
#
# @see https://docs.microsoft.com/en-us/windows/desktop/TaskSchd/idlesettings#properties
#
- IdleSettings = %i[idle_duration restart_on_idle stop_on_idle_end wait_timeout]
+ IdleSettings = %i{idle_duration restart_on_idle stop_on_idle_end wait_timeout}.freeze
# Configures tasks settings
#
# @param [Hash] settings_hash The settings to configure a task
# @option settings_hash [Boolean] :allow_demand_start The subject
@@ -1143,30 +1121,29 @@
# Some modification is required in user input
hash = settings_hash.dup
# Conversion of few settings
hash[:execution_time_limit] = hash[:max_run_time] unless hash[:max_run_time].nil?
- %i[execution_time_limit idle_duration restart_interval wait_timeout].each do |setting|
+ %i{execution_time_limit idle_duration restart_interval wait_timeout}.each do |setting|
hash[setting] = "PT#{hash[setting]}M" unless hash[setting].nil?
end
task_settings = definition.Settings
# Some Idle setting needs to be configured
if IdleSettings.any? { |setting| hash.key?(setting) }
idle_settings = task_settings.IdleSettings
IdleSettings.each do |setting|
- unless hash[setting].nil?
- idle_settings.setproperty(camelize(setting.to_s), hash[setting])
- # This setting is not required to be configured now
- hash.delete(setting)
- end
+ next if hash[setting].nil?
+ idle_settings.setproperty(camelize(setting.to_s), hash[setting])
+ # This setting is not required to be configured now
+ hash.delete(setting)
end
end
# XML settings are not to be configured
- %i[xml_text xml].map { |x| hash.delete(x) }
+ %i{xml_text xml}.map { |x| hash.delete(x) }
hash.each do |setting, value|
setting = camelize(setting.to_s)
definition.Settings.setproperty(setting, value)
end
@@ -1311,30 +1288,30 @@
# @param [String] str
#
# @return [String] In camel case format
#
def camelize(str)
- str.split('_').map(&:capitalize).join
+ str.split("_").map(&:capitalize).join
end
# Converts all the keys of a hash to underscored-symbol format
def symbolize_keys(hash)
hash.each_with_object({}) do |(k, v), h|
h[underscore(k.to_s).to_sym] = v.is_a?(Hash) ? symbolize_keys(v) : v
end
end
def valid_trigger_option(trigger_type)
- [ TASK_TIME_TRIGGER_ONCE, TASK_TIME_TRIGGER_DAILY, TASK_TIME_TRIGGER_WEEKLY,
- TASK_TIME_TRIGGER_MONTHLYDATE, TASK_TIME_TRIGGER_MONTHLYDOW, TASK_EVENT_TRIGGER_ON_IDLE,
- TASK_EVENT_TRIGGER_AT_SYSTEMSTART, TASK_EVENT_TRIGGER_AT_LOGON ].include?(trigger_type.to_i)
+ [TASK_TIME_TRIGGER_ONCE, TASK_TIME_TRIGGER_DAILY, TASK_TIME_TRIGGER_WEEKLY,
+ TASK_TIME_TRIGGER_MONTHLYDATE, TASK_TIME_TRIGGER_MONTHLYDOW, TASK_EVENT_TRIGGER_ON_IDLE,
+ TASK_EVENT_TRIGGER_AT_SYSTEMSTART, TASK_EVENT_TRIGGER_AT_LOGON].include?(trigger_type.to_i)
end
def validate_trigger(hash)
- [:start_year, :start_month, :start_day].each{ |key|
+ %i{start_year start_month start_day}.each do |key|
raise ArgumentError, "#{key} must be set" unless hash[key]
- }
+ end
end
# Configurable settings options
#
# @note Logically, this is summation of
@@ -1343,21 +1320,21 @@
# * :max_run_time, :xml
#
# @return [Array]
#
def valid_settings_options
- %i[allow_demand_start allow_hard_terminate compatibility delete_expired_task_after
+ %i{allow_demand_start allow_hard_terminate compatibility delete_expired_task_after
disallow_start_if_on_batteries disallow_start_on_remote_app_session enabled
execution_time_limit hidden idle_duration maintenance_settings max_run_time
multiple_instances network_settings priority restart_count restart_interval
restart_on_idle run_only_if_idle run_only_if_network_available
start_when_available stop_if_going_on_batteries stop_on_idle_end
- use_unified_scheduling_engine volatile wait_timeout wake_to_run xml xml_text]
+ use_unified_scheduling_engine volatile wait_timeout wake_to_run xml xml_text}
end
def check_for_active_task
- raise Error, 'No currently active task' if @task.nil?
+ raise Error, "No currently active task" if @task.nil?
end
# Checks if the user belongs to service accounts category
#
# @return [Boolean] True or False
@@ -1380,29 +1357,26 @@
#
def system_user?(user)
SYSTEM_USERS.include?(user.to_s.upcase)
end
- # Checks whether the given set of user_id and password suits our requirements.
+ # System users will not require a password
+ # Other users will require a password if the task is non-interactive.
#
- # Raises the error in case of any failures
- #
- # Password should be nil for System Users. For other users, it is required.
- #
# @param [String] user_id
# @param [String] password
#
def check_credential_requirements(user_id, password)
user_id = user_id.to_s
password = password.to_s
- # Password will be required for non-system users
+
if password.empty?
- unless system_user?(user_id)
- raise Error, 'Password is required for non-system users'
+ unless system_user?(user_id) || @interactive
+ raise ArgumentError, "Password is required for non-system users"
end
else
if system_user?(user_id)
- raise Error, 'Password is not required for system users'
+ raise ArgumentError, "Password is not required for system users"
end
end
end
# Returns the applicable flag as per the given users and groups which is used while