lib/win32/taskscheduler.rb in win32-taskscheduler-1.0.2 vs lib/win32/taskscheduler.rb in win32-taskscheduler-1.0.9
- old
+ new
@@ -119,19 +119,24 @@
# :startdoc:
attr_accessor :password
attr_reader :host
+
+ def root_path(path = '\\')
+ path
+ end
+
# Returns a new TaskScheduler object, attached to +folder+. If that
# folder does not exist, but the +force+ option is set to true, then
# it will be created. Otherwise an error will be raised. The default
# is to use the root folder.
#
# If +task+ and +trigger+ are present, then a new task is generated
# as well. This is effectively the same as .new + #new_work_item.
#
- def initialize(task = nil, trigger = nil, folder = "\\", force = false)
+ def initialize(task = nil, trigger = nil, folder = root_path, force = false)
@folder = folder
@force = force
@host = Socket.gethostname
@task = nil
@@ -149,18 +154,17 @@
raise Error, err.inspect
end
@service.Connect
- if folder != "\\"
+ if folder != root_path
begin
@root = @service.GetFolder(folder)
rescue WIN32OLERuntimeError => err
if force
- @root = @service.GetFolder("\\")
- @root.CreateFolder(folder)
- @root = @service.GetFolder(folder)
+ @root = @service.GetFolder(root_path)
+ @root = @root.CreateFolder(folder)
else
raise ArgumentError, "folder '#{folder}' not found"
end
end
else
@@ -195,16 +199,17 @@
path = nil
task_name = nil
# Used the splat operator to put all folder elements into path and leave only the task name`
if full_task_path.include?("\\")
- *path, task_name = full_task_path.split("\\")
+ full_task_path = root_path + full_task_path
+ *path, task_name = full_task_path.split('\\')
else
task_name = full_task_path
end
- folder = path.nil? ? "\\" : path.join("\\")
+ folder = path.nil? ? root_path : path.join("\\")
begin
root = @service.GetFolder(folder)
rescue WIN32OLERuntimeError => err
return false
@@ -459,36 +464,36 @@
update_task_definition(definition)
dir
end
- # Returns the task's priority level. Possible values are 'idle',
- # 'normal', 'high', 'realtime', 'below_normal', 'above_normal',
- # and 'unknown'.
+ # Returns the task's priority level. Possible values are 'idle', 'lowest'.
+ # 'below_normal_8', 'below_normal_7', 'normal_6', 'normal_5', 'normal_4',
+ # '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'
+ priority = 'above_normal_2'
when 3
- priority = 'above_normal'
+ priority = 'above_normal_3'
when 4
- priority = 'normal'
+ priority = 'normal_4'
when 5
- priority = 'normal'
+ priority = 'normal_5'
when 6
- priority = 'normal'
+ priority = 'normal_6'
when 7
- priority = 'below_normal'
+ priority = 'below_normal_7'
when 8
- priority = 'below_normal'
+ priority = 'below_normal_8'
when 9
priority = 'lowest'
when 10
priority = 'idle'
else
@@ -1176,11 +1181,11 @@
definition.Settings.AllowDemandStart = allow_demand_start if allow_demand_start
definition.Settings.AllowHardTerminate = allow_hard_terminate if allow_hard_terminate
definition.Settings.Compatibility = compatibility if compatibility
definition.Settings.DeleteExpiredTaskAfter = delete_expired_task_after if delete_expired_task_after
- definition.Settings.DisallowStartIfOnBatteries = disallow_start_if_on_batteries if disallow_start_if_on_batteries
+ definition.Settings.DisallowStartIfOnBatteries = disallow_start_if_on_batteries if !disallow_start_if_on_batteries.nil?
definition.Settings.Enabled = enabled if enabled
definition.Settings.ExecutionTimeLimit = execution_time_limit if execution_time_limit
definition.Settings.Hidden = hidden if hidden
definition.Settings.IdleSettings.IdleDuration = idle_duration if idle_duration
definition.Settings.IdleSettings.StopOnIdleEnd = stop_on_idle_end if stop_on_idle_end
@@ -1191,11 +1196,11 @@
definition.Settings.RestartCount = restart_count if restart_count
definition.Settings.RestartInterval = restart_interval if restart_interval
definition.Settings.RunOnlyIfIdle = run_only_if_idle if run_only_if_idle
definition.Settings.RunOnlyIfNetworkAvailable = run_only_if_network_available if run_only_if_network_available
definition.Settings.StartWhenAvailable = start_when_available if start_when_available
- definition.Settings.StopIfGoingOnBatteries = stop_if_going_on_batteries if stop_if_going_on_batteries
+ definition.Settings.StopIfGoingOnBatteries = stop_if_going_on_batteries if !stop_if_going_on_batteries.nil?
definition.Settings.WakeToRun = wake_to_run if wake_to_run
definition.Settings.XmlText = xml_text if xml_text
update_task_definition(definition)
@@ -1309,10 +1314,14 @@
settings_hash[setting.name] = @task.Definition.Settings.NetworkSettings._getproperty(setting.dispid, [], [])
end
symbolize_keys(settings_hash)
end
+ def task_user_id(definition)
+ definition.Principal.UserId
+ end
+
private
# Returns a camle-case string to its underscore format
def underscore(string)
string.gsub(/([a-z\d])([A-Z])/, '\1_\2'.freeze).downcase
@@ -1340,11 +1349,10 @@
def check_for_active_task
raise Error, 'No currently active task' if @task.nil?
end
def update_task_definition(definition)
- user = definition.Principal.UserId
-
+ user = task_user_id(definition) || 'SYSTEM'
@task = @root.RegisterTaskDefinition(
@task.Path,
definition,
TASK_CREATE_OR_UPDATE,
user,