Class: Win32::TaskScheduler

Inherits:
Object show all
Extended by:
FFI::Library
Includes:
Puppet::Util::Windows::String
Defined in:
lib/puppet/util/windows/taskscheduler.rb

Overview

The TaskScheduler class encapsulates taskscheduler settings and behavior

Defined Under Namespace

Modules: COM Classes: Error

Constant Summary collapse

IDLE =

Shorthand constants

Puppet::Util::Windows::Process::IDLE_PRIORITY_CLASS
NORMAL =
Puppet::Util::Windows::Process::NORMAL_PRIORITY_CLASS
HIGH =
Puppet::Util::Windows::Process::HIGH_PRIORITY_CLASS
REALTIME =
Puppet::Util::Windows::Process::REALTIME_PRIORITY_CLASS
BELOW_NORMAL =
Puppet::Util::Windows::Process::BELOW_NORMAL_PRIORITY_CLASS
ABOVE_NORMAL =
Puppet::Util::Windows::Process::ABOVE_NORMAL_PRIORITY_CLASS
ONCE =
TASK_TIME_TRIGGER_ONCE
DAILY =
TASK_TIME_TRIGGER_DAILY
WEEKLY =
TASK_TIME_TRIGGER_WEEKLY
MONTHLYDATE =
TASK_TIME_TRIGGER_MONTHLYDATE
MONTHLYDOW =
TASK_TIME_TRIGGER_MONTHLYDOW
ON_IDLE =
TASK_EVENT_TRIGGER_ON_IDLE
AT_SYSTEMSTART =
TASK_EVENT_TRIGGER_AT_SYSTEMSTART
AT_LOGON =
TASK_EVENT_TRIGGER_AT_LOGON
FIRST_WEEK =
TASK_FIRST_WEEK
SECOND_WEEK =
TASK_SECOND_WEEK
THIRD_WEEK =
TASK_THIRD_WEEK
FOURTH_WEEK =
TASK_FOURTH_WEEK
LAST_WEEK =
TASK_LAST_WEEK
SUNDAY =
TASK_SUNDAY
MONDAY =
TASK_MONDAY
TUESDAY =
TASK_TUESDAY
WEDNESDAY =
TASK_WEDNESDAY
THURSDAY =
TASK_THURSDAY
FRIDAY =
TASK_FRIDAY
SATURDAY =
TASK_SATURDAY
JANUARY =
TASK_JANUARY
FEBRUARY =
TASK_FEBRUARY
MARCH =
TASK_MARCH
APRIL =
TASK_APRIL
MAY =
TASK_MAY
JUNE =
TASK_JUNE
JULY =
TASK_JULY
AUGUST =
TASK_AUGUST
SEPTEMBER =
TASK_SEPTEMBER
OCTOBER =
TASK_OCTOBER
NOVEMBER =
TASK_NOVEMBER
DECEMBER =
TASK_DECEMBER
INTERACTIVE =
TASK_FLAG_INTERACTIVE
DELETE_WHEN_DONE =
TASK_FLAG_DELETE_WHEN_DONE
DISABLED =
TASK_FLAG_DISABLED
START_ONLY_IF_IDLE =
TASK_FLAG_START_ONLY_IF_IDLE
KILL_ON_IDLE_END =
TASK_FLAG_KILL_ON_IDLE_END
DONT_START_IF_ON_BATTERIES =
TASK_FLAG_DONT_START_IF_ON_BATTERIES
KILL_IF_GOING_ON_BATTERIES =
TASK_FLAG_KILL_IF_GOING_ON_BATTERIES
RUN_ONLY_IF_DOCKED =
TASK_FLAG_RUN_ONLY_IF_DOCKED
HIDDEN =
TASK_FLAG_HIDDEN
RUN_IF_CONNECTED_TO_INTERNET =
TASK_FLAG_RUN_IF_CONNECTED_TO_INTERNET
RESTART_ON_IDLE_RESUME =
TASK_FLAG_RESTART_ON_IDLE_RESUME
SYSTEM_REQUIRED =
TASK_FLAG_SYSTEM_REQUIRED
RUN_ONLY_IF_LOGGED_ON =
TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
FLAG_HAS_END_DATE =
TASK_TRIGGER_FLAG_HAS_END_DATE
FLAG_KILL_AT_DURATION_END =
TASK_TRIGGER_FLAG_KILL_AT_DURATION_END
FLAG_DISABLED =
TASK_TRIGGER_FLAG_DISABLED
MAX_RUN_TIMES =
TASK_MAX_RUN_TIMES

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from FFI::Library

attach_function_private

Methods included from Puppet::Util::Windows::String

wide_string

Constructor Details

#initialize(work_item = nil, trigger = nil) ⇒ TaskScheduler

Returns a new TaskScheduler object. If a work_item (and possibly the the trigger) are passed as arguments then a new work item is created and associated with that trigger, although you can still activate other tasks with the same handle.

This is really just a bit of convenience. Passing arguments to the constructor is the same as calling TaskScheduler.new plus TaskScheduler#new_work_item.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/puppet/util/windows/taskscheduler.rb', line 170

def initialize(work_item=nil, trigger=nil)
  @pITS   = nil
  @pITask = nil

  if ! self.class.com_initialized
    Puppet::Util::Windows::COM.InitializeCom()
    self.class.com_initialized = true
  end

  @pITS = COM::TaskScheduler.new
  at_exit do
    begin
      @pITS.Release if @pITS && !@pITS.null?
      @pITS = nil
    rescue
    end
  end

  if work_item
    if trigger
      raise TypeError unless trigger.is_a?(Hash)
      new_work_item(work_item, trigger)
    end
  end
end

Class Attribute Details

.com_initializedObject

Returns the value of attribute com_initialized.



18
19
20
# File 'lib/puppet/util/windows/taskscheduler.rb', line 18

def com_initialized
  @com_initialized
end

Instance Method Details

#account_informationObject

Returns the user associated with the task or nil if no user has yet been associated with the task.

Raises:



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/puppet/util/windows/taskscheduler.rb', line 342

def 
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  # default under certain failures
  user = nil

  begin
    FFI::MemoryPointer.new(:pointer) do |ptr|
      @pITask.GetAccountInformation(ptr)
      ptr.read_com_memory_pointer do |str_ptr|
        user = str_ptr.read_arbitrary_wide_string_up_to(256) if ! str_ptr.null?
      end
    end
  rescue Puppet::Util::Windows::Error => e
    raise e unless e.code ==  ||
                   e.code == SCHED_E_NO_SECURITY_SERVICES ||
                   e.code == ERROR_NONE_MAPPED
  end

  user
end

#activate(task) ⇒ Object

Activate the specified task.

Raises:



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/puppet/util/windows/taskscheduler.rb', line 231

def activate(task)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise TypeError unless task.is_a?(String)

  FFI::MemoryPointer.new(:pointer) do |ptr|
    @pITS.Activate(wide_string(task), IID_ITask, ptr)

    reset_current_task
    @pITask = COM::Task.new(ptr.read_pointer)
  end

  @pITask
end

#add_trigger(index, trigger) ⇒ Object

Adds a trigger at the specified index.

Raises:



609
610
611
612
613
614
615
616
617
# File 'lib/puppet/util/windows/taskscheduler.rb', line 609

def add_trigger(index, trigger)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?
  raise TypeError unless trigger.is_a?(Hash)

  @pITask.UseInstance(COM::TaskTrigger, :GetTrigger, index) do |pITaskTrigger|
    populate_trigger(pITaskTrigger, trigger)
  end
end

#application_nameObject

Returns the name of the application associated with the task.

Raises:



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/puppet/util/windows/taskscheduler.rb', line 367

def application_name
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  app = nil

  FFI::MemoryPointer.new(:pointer) do |ptr|
    @pITask.GetApplicationName(ptr)

    ptr.read_com_memory_pointer do |str_ptr|
      app = str_ptr.read_arbitrary_wide_string_up_to(256) if ! str_ptr.null?
    end
  end

  app
end

#application_name=(app) ⇒ Object

Sets the application name associated with the task.

Raises:



386
387
388
389
390
391
392
393
394
# File 'lib/puppet/util/windows/taskscheduler.rb', line 386

def application_name=(app)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?
  raise TypeError unless app.is_a?(String)

  @pITask.SetApplicationName(wide_string(app))

  app
end

#commentObject

Returns the comment associated with the task, if any.

Raises:



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/puppet/util/windows/taskscheduler.rb', line 693

def comment
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  comment = nil

  FFI::MemoryPointer.new(:pointer) do |ptr|
    @pITask.GetComment(ptr)

    ptr.read_com_memory_pointer do |str_ptr|
      comment = str_ptr.read_arbitrary_wide_string_up_to(256) if ! str_ptr.null?
    end
  end

  comment
end

#comment=(comment) ⇒ Object

Sets the comment for the task.

Raises:



711
712
713
714
715
716
717
# File 'lib/puppet/util/windows/taskscheduler.rb', line 711

def comment=(comment)
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?
  raise TypeError unless comment.is_a?(String)

  @pITask.SetComment(wide_string(comment))
  comment
end

#creatorObject

Returns the name of the user who created the task.

Raises:



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/puppet/util/windows/taskscheduler.rb', line 721

def creator
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  creator = nil

  FFI::MemoryPointer.new(:pointer) do |ptr|
    @pITask.GetCreator(ptr)

    ptr.read_com_memory_pointer do |str_ptr|
      creator = str_ptr.read_arbitrary_wide_string_up_to(256) if ! str_ptr.null?
    end
  end

  creator
end

#creator=(creator) ⇒ Object

Sets the creator for the task.

Raises:



739
740
741
742
743
744
745
# File 'lib/puppet/util/windows/taskscheduler.rb', line 739

def creator=(creator)
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?
  raise TypeError unless creator.is_a?(String)

  @pITask.SetCreator(wide_string(creator))
  creator
end

#delete(task) ⇒ Object

Delete the specified task name.

Raises:



247
248
249
250
251
252
253
254
# File 'lib/puppet/util/windows/taskscheduler.rb', line 247

def delete(task)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise TypeError unless task.is_a?(String)

  @pITS.Delete(wide_string(task))

  true
end

#delete_trigger(index) ⇒ Object

Deletes the trigger at the specified index.

Raises:



560
561
562
563
564
565
566
# File 'lib/puppet/util/windows/taskscheduler.rb', line 560

def delete_trigger(index)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  @pITask.DeleteTrigger(index)
  index
end

#enumObject Also known as: tasks

Returns an array of scheduled task names.

Raises:



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/puppet/util/windows/taskscheduler.rb', line 198

def enum
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  array = []

  @pITS.UseInstance(COM::EnumWorkItems, :Enum) do |pIEnum|
    FFI::MemoryPointer.new(:pointer) do |names_array_ptr_ptr|
      FFI::MemoryPointer.new(:win32_ulong) do |fetched_count_ptr|
        # awkward usage, if number requested is available, returns S_OK (0), or if less were returned returns S_FALSE (1)
        while (pIEnum.Next(TASKS_TO_RETRIEVE, names_array_ptr_ptr, fetched_count_ptr) >= Puppet::Util::Windows::COM::S_OK)
          count = fetched_count_ptr.read_win32_ulong
          break if count == 0

          names_array_ptr_ptr.read_com_memory_pointer do |names_array_ptr|
            # iterate over the array of pointers
            name_ptr_ptr = FFI::Pointer.new(:pointer, names_array_ptr)
            for i in 0 ... count
              name_ptr_ptr[i].read_com_memory_pointer do |name_ptr|
                array << name_ptr.read_arbitrary_wide_string_up_to(256)
              end
            end
          end
        end
      end
    end
  end

  array
end

#exists?(job_name) ⇒ Boolean

Returns whether or not the scheduled task exists.

Returns:

  • (Boolean)


810
811
812
813
814
815
816
817
818
819
# File 'lib/puppet/util/windows/taskscheduler.rb', line 810

def exists?(job_name)
  bool = false
  Dir.foreach("#{ENV['SystemRoot']}/Tasks"){ |file|
    if File.basename(file, '.job') == job_name
      bool = true
      break
    end
  }
  bool
end

#exit_codeObject

Returns the exit code from the last scheduled run.

Raises:



674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/puppet/util/windows/taskscheduler.rb', line 674

def exit_code
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  status = 0

  begin
    FFI::MemoryPointer.new(:dword, 1) do |ptr|
      @pITask.GetExitCode(ptr)
      status = ptr.read_dword
    end
  rescue Puppet::Util::Windows::Error => e
    raise e unless e.code == SCHED_S_TASK_HAS_NOT_RUN
  end

  status
end

#flagsObject

Returns the flags (integer) that modify the behavior of the work item. You must OR the return value to determine the flags yourself.

Raises:



622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/puppet/util/windows/taskscheduler.rb', line 622

def flags
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  flags = 0

  FFI::MemoryPointer.new(:dword, 1) do |ptr|
    @pITask.GetFlags(ptr)
    flags = ptr.read_dword
  end

  flags
end

#flags=(flags) ⇒ Object

Sets an OR’d value of flags that modify the behavior of the work item.

Raises:



637
638
639
640
641
642
643
# File 'lib/puppet/util/windows/taskscheduler.rb', line 637

def flags=(flags)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  @pITask.SetFlags(flags)
  flags
end

#machine=(host) ⇒ Object Also known as: host=

Set the host on which the various TaskScheduler methods will execute.

Raises:



296
297
298
299
300
301
302
303
# File 'lib/puppet/util/windows/taskscheduler.rb', line 296

def machine=(host)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise TypeError unless host.is_a?(String)

  @pITS.SetTargetComputer(wide_string(host))

  host
end

#max_run_timeObject

Returns the maximum length of time, in milliseconds, that the task will run before terminating.

Raises:



785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/puppet/util/windows/taskscheduler.rb', line 785

def max_run_time
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  max_run_time = nil

  FFI::MemoryPointer.new(:dword, 1) do |ptr|
    @pITask.GetMaxRunTime(ptr)
    max_run_time = ptr.read_dword
  end

  max_run_time
end

#max_run_time=(max_run_time) ⇒ Object

Sets the maximum length of time, in milliseconds, that the task can run before terminating. Returns the value you specified if successful.

Raises:



801
802
803
804
805
806
807
# File 'lib/puppet/util/windows/taskscheduler.rb', line 801

def max_run_time=(max_run_time)
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?
  raise TypeError unless max_run_time.is_a?(Numeric)

  @pITask.SetMaxRunTime(max_run_time)
  max_run_time
end

#most_recent_run_timeObject

Returns a Time object indicating the most recent time the task ran or nil if the task has never run.

Raises:



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/puppet/util/windows/taskscheduler.rb', line 765

def most_recent_run_time
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  time = nil

  begin
    FFI::MemoryPointer.new(WIN32::SYSTEMTIME.size) do |ptr|
      @pITask.GetMostRecentRunTime(ptr)
      time = WIN32::SYSTEMTIME.new(ptr).to_local_time
    end
  rescue Puppet::Util::Windows::Error => e
    raise e unless e.code == SCHED_S_TASK_HAS_NOT_RUN
  end

  time
end

#new_work_item(task, trigger) ⇒ Object Also known as: new_task

Creates a new work item (scheduled job) with the given trigger. The trigger variable is a hash of options that define when the scheduled job should run.

Raises:

  • (TypeError)


509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/puppet/util/windows/taskscheduler.rb', line 509

def new_work_item(task, trigger)
  raise TypeError unless trigger.is_a?(Hash)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?

  # I'm working around github issue #1 here.
  enum.each{ |name|
    if name.downcase == task.downcase + '.job'
      raise Error.new("task '#{task}' already exists")
    end
  }

  FFI::MemoryPointer.new(:pointer) do |ptr|
    @pITS.NewWorkItem(wide_string(task), CLSID_CTask, IID_ITask, ptr)

    reset_current_task
    @pITask = COM::Task.new(ptr.read_pointer)

    FFI::MemoryPointer.new(:word, 1) do |trigger_index_ptr|
      # Without the 'enum.include?' check above the code segfaults here if the
      # task already exists. This should probably be handled properly instead
      # of simply avoiding the issue.

      @pITask.UseInstance(COM::TaskTrigger, :CreateTrigger, trigger_index_ptr) do |pITaskTrigger|
        populate_trigger(pITaskTrigger, trigger)
      end
    end
  end

  @pITask
end

#next_run_timeObject

Returns a Time object that indicates the next time the task will run.

Raises:



749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/puppet/util/windows/taskscheduler.rb', line 749

def next_run_time
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  time = nil

  FFI::MemoryPointer.new(WIN32::SYSTEMTIME.size) do |ptr|
    @pITask.GetNextRunTime(ptr)
    time = WIN32::SYSTEMTIME.new(ptr).to_local_time
  end

  time
end

#parametersObject

Returns the command line parameters for the task.

Raises:



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/puppet/util/windows/taskscheduler.rb', line 398

def parameters
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  param = nil

  FFI::MemoryPointer.new(:pointer) do |ptr|
    @pITask.GetParameters(ptr)

    ptr.read_com_memory_pointer do |str_ptr|
      param = str_ptr.read_arbitrary_wide_string_up_to(256) if ! str_ptr.null?
    end
  end

  param
end

#parameters=(param) ⇒ Object

Sets the parameters for the task. These parameters are passed as command line arguments to the application the task will run. To clear the command line parameters set it to an empty string.

Raises:



419
420
421
422
423
424
425
426
427
# File 'lib/puppet/util/windows/taskscheduler.rb', line 419

def parameters=(param)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?
  raise TypeError unless param.is_a?(String)

  @pITask.SetParameters(wide_string(param))

  param
end

#priorityObject

Returns the task’s priority level. Possible values are ‘idle’, ‘normal’, ‘high’, ‘realtime’, ‘below_normal’, ‘above_normal’, and ‘unknown’.

Raises:



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/puppet/util/windows/taskscheduler.rb', line 464

def priority
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  FFI::MemoryPointer.new(:dword, 1) do |ptr|
    @pITask.GetPriority(ptr)

    pri = ptr.read_dword
    if (pri & IDLE) != 0
      priority = 'idle'
    elsif (pri & NORMAL) != 0
      priority = 'normal'
    elsif (pri & HIGH) != 0
      priority = 'high'
    elsif (pri & REALTIME) != 0
      priority = 'realtime'
    elsif (pri & BELOW_NORMAL) != 0
      priority = 'below_normal'
    elsif (pri & ABOVE_NORMAL) != 0
      priority = 'above_normal'
    else
      priority = 'unknown'
    end
  end

  priority
end

#priority=(priority) ⇒ Object

Sets the priority of the task. The priority should be a numeric priority constant value.

Raises:



495
496
497
498
499
500
501
502
503
# File 'lib/puppet/util/windows/taskscheduler.rb', line 495

def priority=(priority)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?
  raise TypeError unless priority.is_a?(Numeric)

  @pITask.SetPriority(priority)

  priority
end

#runObject

Execute the current task.

Raises:



258
259
260
261
262
# File 'lib/puppet/util/windows/taskscheduler.rb', line 258

def run
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  @pITask.Run
end

#save(file = nil) ⇒ Object

Saves the current task. Tasks must be saved before they can be activated. The .job file itself is typically stored in the C:WINDOWSTasks folder.

If file (an absolute path) is specified then the job is saved to that file instead. A ‘.job’ extension is recommended but not enforced.

Raises:



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/puppet/util/windows/taskscheduler.rb', line 270

def save(file = nil)
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  reset = true

  begin
    @pITask.QueryInstance(COM::PersistFile) do |pIPersistFile|
      pIPersistFile.Save(wide_string(file), 1)
    end
  rescue
    reset = false
  ensure
    reset_current_task if reset
  end
end

#set_account_information(user, password) ⇒ Object

Sets the user and password for the given task. If the user and password are set properly then true is returned.

In some cases the job may be created, but the account information was bad. In this case the task is created but a warning is generated and false is returned.

Raises:



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/puppet/util/windows/taskscheduler.rb', line 314

def (user, password)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  bool = false

  begin
    if (user.nil? || user=="") && (password.nil? || password=="")
      @pITask.SetAccountInformation(wide_string(""), FFI::Pointer::NULL)
    else
      user = wide_string(user)
      password = wide_string(password)
      @pITask.SetAccountInformation(user, password)
    end

    bool = true
  rescue Puppet::Util::Windows::Error => e
    raise e unless e.code == 

    warn 'job created, but password was invalid'
  end

  bool
end

#statusObject

Returns the status of the currently active task. Possible values are ‘ready’, ‘running’, ‘not scheduled’ or ‘unknown’.

Raises:



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/puppet/util/windows/taskscheduler.rb', line 648

def status
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  st = nil

  FFI::MemoryPointer.new(:hresult, 1) do |ptr|
    @pITask.GetStatus(ptr)
    st = ptr.read_hresult
  end

  case st
    when SCHED_S_TASK_READY
       status = 'ready'
    when SCHED_S_TASK_RUNNING
       status = 'running'
    when SCHED_S_TASK_NOT_SCHEDULED
       status = 'not scheduled'
    else
       status = 'unknown'
  end

  status
end

#terminateObject

Terminate the current task.

Raises:



288
289
290
291
292
# File 'lib/puppet/util/windows/taskscheduler.rb', line 288

def terminate
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  @pITask.Terminate
end

#trigger(index) ⇒ Object

Returns a hash that describes the trigger at the given index for the current task.

Raises:



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/puppet/util/windows/taskscheduler.rb', line 571

def trigger(index)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  trigger = {}

  @pITask.UseInstance(COM::TaskTrigger, :GetTrigger, index) do |pITaskTrigger|
    FFI::MemoryPointer.new(COM::TASK_TRIGGER.size) do |task_trigger_ptr|
      pITaskTrigger.GetTrigger(task_trigger_ptr)
      trigger = populate_hash_from_trigger(COM::TASK_TRIGGER.new(task_trigger_ptr))
    end
  end

  trigger
end

#trigger=(trigger) ⇒ Object

Sets the trigger for the currently active task.

Raises:



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/puppet/util/windows/taskscheduler.rb', line 589

def trigger=(trigger)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?
  raise TypeError unless trigger.is_a?(Hash)

  FFI::MemoryPointer.new(:word, 1) do |trigger_index_ptr|
    # Without the 'enum.include?' check above the code segfaults here if the
    # task already exists. This should probably be handled properly instead
    # of simply avoiding the issue.

    @pITask.UseInstance(COM::TaskTrigger, :CreateTrigger, trigger_index_ptr) do |pITaskTrigger|
      populate_trigger(pITaskTrigger, trigger)
    end
  end

  trigger
end

#trigger_countObject

Returns the number of triggers associated with the active task.

Raises:



544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/puppet/util/windows/taskscheduler.rb', line 544

def trigger_count
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  count = 0

  FFI::MemoryPointer.new(:word, 1) do |ptr|
    @pITask.GetTriggerCount(ptr)
    count = ptr.read_word
  end

  count
end

#working_directoryObject

Returns the working directory for the task.

Raises:



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/puppet/util/windows/taskscheduler.rb', line 431

def working_directory
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?

  dir = nil

  FFI::MemoryPointer.new(:pointer) do |ptr|
    @pITask.GetWorkingDirectory(ptr)

    ptr.read_com_memory_pointer do |str_ptr|
      dir = str_ptr.read_arbitrary_wide_string_up_to(256) if ! str_ptr.null?
    end
  end

  dir
end

#working_directory=(dir) ⇒ Object

Sets the working directory for the task.

Raises:



450
451
452
453
454
455
456
457
458
# File 'lib/puppet/util/windows/taskscheduler.rb', line 450

def working_directory=(dir)
  raise Error.new('No current task scheduler. ITaskScheduler is NULL.') if @pITS.nil?
  raise Error.new('No currently active task. ITask is NULL.') if @pITask.nil?
  raise TypeError unless dir.is_a?(String)

  @pITask.SetWorkingDirectory(wide_string(dir))

  dir
end