Jump to content

Creating a "brushed up" Task Scheduler UDF?


water
 Share

Would you like to see a brushed up Task Scheduler UDF (inlcuding help files, examples ...)?  

36 members have voted

  1. 1. Would you like to see such a brushed up UDF?

    • Yes
    • Don't mind
    • Maybe later
      0
    • No
      0


Recommended Posts

In _TS_ActionCreate we do no longer need to call _TS_TaskGet which means parameter $oService is no longer required when calling _TS_ActionCreate.
Dropping this parameter would be a Script Breaking change.

What do you think, should I drop the unneeded parameter?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

So you plan on making _TS_ActionCreate only take the task definition object, and not a task path?  if that is the case, I see no need to have the $oService parameter, even if it is a script breaking change.  Wouldn't this also be the case with _TS_TriggerCreate, as it returns a trigger object, and uses _TS_TaskGet.  I haven't got into testing creating triggers yet.  I just noticed the code was similar.  Now look more through the UDF, I also noticed other functions that take $vTask, use _TS_TaskGet, and return an object.  I'll need to test some more with existing tasks, but I won't be able to until starting on Monday.  I will say, I like having the option to use the task path or the task object for the other functions.  There may be a way to still be able to use the task path.  If not, would could go all object inputs.  

Get you some rest, and have a good weekend.

 

Adam

 

Link to comment
Share on other sites

Yes, just have been playing with _TS_ActionCreate. As soon as we have decided how the function should work (means: only accepts a Task Definition object or accepts a Task path, a Task Object and a Task Definition object) this design should then be applied to all functions.
Having some time to think about the concept is a good idea - so have a nice weekend :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I played a bit with _TS_ActionCreate but could only get the Task Definition parameter to work. The Task object didn't work, the Task path did neither.
So I think we need to stick with the Task Definition object as parameter.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I think I will modify the UDF to work with the Definition object only and drop the Task object and Task path.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I will check all functions. At the moment I think I will start - as you suggested - with the _TS_Action* and _TS_Trigger* functions.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I have already updated all functions that create/delete objects: _TS_ActionCreate, _TS_ActionDelete,  _TS_TriggerCreate and _TS_TriggerDelete.

Will check your list quite soon :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I have released version 1.4.0.0 for you to play with.
1.3.0.0 is still available for download.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks water for the updated UDF.  I ran the new UDF with my actions test scripts, and the doesn't seem to be an issue.  I have now started working with triggers, and I have noticed an issue right off with _TS_TaskPropertiesGet.  It is only showing the first trigger.  Manually define multiple triggers for Test-Task, try to get to the listed triggers.  Test script below.  

#RequireAdmin

#include <Array.au3>
#include "TaskScheduler.au3"

Global $sTaskName = "Test-Task"
Global $sTaskFolder = "\Test"
Global $sTask = $sTaskFolder & (($sTaskFolder = "\") ? ("") : ("\")) & $sTaskName
Global $oTask = Null
Global $oTaskDefinition = Null
Global $vTaskProperties = 0

; Connect to the Task Scheduler Service.
Global $oService = _TS_Open()

; Get Task Object.
$oTask = _TS_TaskGet($oService, $sTask)
;~ $oTask = _TS_TaskGet($oService, $sTaskNew)
If Not @error Then
    MsgBox($MB_ICONINFORMATION, "_TS_TaskGet", "Task found:" & @CRLF & "  Name: " & $oTask.Name & @CRLF & "  Path: " & $oTask.Path)
Else
    MsgBox($MB_ICONERROR, "_TS_TaskGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf

; Get Task Definition.
$oTaskDefinition = _TS_TaskGet($oService, $sTask, 1)
If Not @error Then
    MsgBox($MB_ICONINFORMATION, "_TS_TaskGet", "Task Definition found.")
Else
    MsgBox($MB_ICONERROR, "_TS_TaskGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf

; Check Triggers.
;~ $vTaskProperties = _TS_TaskPropertiesGet($oService, $sTask, 2, True)
$vTaskProperties = _TS_TaskPropertiesGet($oService, $sTask, 2, True, "TRIGGERS")
;~ $vTaskProperties = _TS_TaskPropertiesGet($oService, $oTask, 2, True, "TRIGGERS")
;~ $vTaskProperties = _TS_TaskPropertiesGet($oService, $oTaskDefinition, 2, True, "TRIGGERS")
;~ $vTaskProperties = _TS_TaskPropertiesGet($oService, $sTask, 2, False, "TRIGGERS")
If @error Then
    MsgBox($MB_ICONERROR, "_TS_TaskPropertiesGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
Else
    If IsArray($vTaskProperties) Then 
        _ArrayDisplay($vTaskProperties, "_TS_TaskPropertiesGet")
    Else
        MsgBox($MB_OK, "$vTaskProperties", $vTaskProperties)
    EndIf 
EndIf

;Test accessing the Trigger Object directly. 
ConsoleWrite("Triggers Count: " & $oTaskDefinition.Triggers.Count & @CRLF)

For $oTrigger In $oTaskDefinition.Triggers
    ConsoleWrite($oTrigger.Type & "  " & $oTrigger.Enabled & " " & $oTrigger.Id & "  " & $oTrigger.Repetition & "  " & $oTrigger.StartBoundary & "  " & $oTrigger.EndBoundary & "  " & $oTrigger.ExecutionTimeLimit & @CRLF)
Next

_TS_Close()

 

Adam

 

Link to comment
Share on other sites

That's a bug. The second Trigger is wrongly named "REPETITION".

Modify block:

$cTaskTriggers = .Triggers
            If IsObj($cTaskTriggers) Then
                $sSection = "TRIGGERS"
                For $oTaskTrigger In $cTaskTriggers
                    With $oTaskTrigger

to:

$cTaskTriggers = .Triggers
            If IsObj($cTaskTriggers) Then
                For $oTaskTrigger In $cTaskTriggers
                    $sSection = "TRIGGERS"
                    With $oTaskTrigger

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hi water,

I found a bug in _TS_TaskPropertiesSet in relation to setting trigger repetition.  The following needs to be changed in the function.  

From: 

Case "Repetition"
                If $iObjectType <> $ObjectType_Trigger Then ContinueLoop     ; Ignore this section when the properties are invalid for the object
                Switch $aTemp[1]
                    Case "Duration"
                        $oTrigger.RepetitionPattern.Duration = $aTemp[2]
                    Case "Interval"
                        $oTrigger.RepetitionPattern.Interval = $aTemp[2]
                    Case "StopAtDurationEnd"
                        $oTrigger.RepetitionPattern.StopAtDurationEnd = $aTemp[2]
                    Case Else
                        Return SetError(2002, $i, 0)
                EndSwitch

To:

Case "Repetition"
                If $iObjectType <> $ObjectType_Trigger Then ContinueLoop     ; Ignore this section when the properties are invalid for the object
                Switch $aTemp[1]
                    Case "Duration"
                        $oTrigger.Repetition.Duration = $aTemp[2]
                    Case "Interval"
                        $oTrigger.Repetition.Interval = $aTemp[2]
                    Case "StopAtDurationEnd"
                        $oTrigger.Repetition.StopAtDurationEnd = $aTemp[2]
                    Case Else
                        Return SetError(2002, $i, 0)
                EndSwitch

 

Adam

 

Link to comment
Share on other sites

Fixed. Will be part of the next version :)

This version will have a _TS_TaskUpdate function (a stripped down version of the _TS_TaskRegister function).

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Version 1.5.0.0 released

Changelog can be found on the download page and the history in the ZIP file.

For download please see my signature below.

Please play with this version and tell me what doesn't work or is missing!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hi water,

I'm testing the new _TS_TaskUpdate function, and I keep getting a get a 3302 error.  I working with deleting triggers.  Create multiple triggers manually.  Example script below.  

#RequireAdmin

#include <Array.au3>
#include <Debug.au3>
#include "TaskScheduler.au3"

Global $sTaskName = "Test-Task"
Global $sTaskFolder = "\Test"
Global $sTask = $sTaskFolder & (($sTaskFolder = "\") ? ("") : ("\")) & $sTaskName
Global $oTask = Null
Global $oTaskDefinition = Null
Global $vTaskProperties = 0

; Connect to the Task Scheduler Service.
Global $oService = _TS_Open()

; Get Task Definition.
$oTaskDefinition = _TS_TaskGet($oService, $sTask, 1)
If Not @error Then
    MsgBox($MB_ICONINFORMATION, "_TS_TaskGet", "Task Definition found.")
Else
    MsgBox($MB_ICONERROR, "_TS_TaskGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf

; Check Triggers.
$vTaskProperties = _TS_TaskPropertiesGet($oService, $sTask, 2, True, "TRIGGERS")
If @error Then
    MsgBox($MB_ICONERROR, "_TS_TaskPropertiesGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
Else
    If IsArray($vTaskProperties) Then 
        _ArrayDisplay($vTaskProperties, "_TS_TaskPropertiesGet")
        ConsoleWrite(_ArrayToString($vTaskProperties, @CRLF) & @CRLF & @CRLF)
    Else
        MsgBox($MB_OK, "$vTaskProperties", $vTaskProperties)
    EndIf 
EndIf

;Test accessing the Trigger Object directly. 
ConsoleWrite("Triggers Count: " & $oTaskDefinition.Triggers.Count & @CRLF)

For $oTrigger In $oTaskDefinition.Triggers
    ConsoleWrite($oTrigger.Type & "  " & $oTrigger.Enabled & " " & $oTrigger.Id & "  " & $oTrigger.Repetition.Duration & "  " & $oTrigger.Repetition.Interval & "  " & _ 
    $oTrigger.Repetition.StopAtDurationEnd & "  " & $oTrigger.StartBoundary & "  " & $oTrigger.EndBoundary & "  " & $oTrigger.ExecutionTimeLimit & @CRLF)
Next

; Delete all but the first trigger.
For $i = $oTaskDefinition.Triggers.Count To 2 Step -1
    _TS_TriggerDelete($oTaskDefinition, $i)
    If @error Then MsgBox($MB_ICONERROR, "_TS_TriggerDelete", "_TS_TriggerDelete returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
Next

$oTask = _TS_TaskGet($oService, $sTask)
If Not @error Then
    MsgBox($MB_ICONINFORMATION, "_TS_TaskGet", "Task found:" & @CRLF & "  Name: " & $oTask.Name & @CRLF & "  Path: " & $oTask.Path)
Else
    MsgBox($MB_ICONERROR, "_TS_TaskGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
EndIf

; Register to update the task.  Works correctly.
;~ $oTask = _TS_TaskRegister($oService, $sTaskFolder, $sTaskName, $oTaskDefinition, Default, Default, Default, $TASK_UPDATE)
;~ If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskRegister", "_TS_TaskRegister returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))

; Update the task.  Does not work correctly.  
$oTask = _TS_TaskUpdate($oService, $oTask)  ;<== Returning 3302.  
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskUpdate", "_TS_TaskUpdate returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))


; Check Triggers.
ConsoleWrite(@CRLF & @CRLF)
$vTaskProperties = _TS_TaskPropertiesGet($oService, $sTask, 2, True, "TRIGGERS")
If @error Then
    MsgBox($MB_ICONERROR, "_TS_TaskPropertiesGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
Else
    If IsArray($vTaskProperties) Then 
        _ArrayDisplay($vTaskProperties, "_TS_TaskPropertiesGet")
        ConsoleWrite(_ArrayToString($vTaskProperties, @CRLF) & @CRLF & @CRLF)
    Else
        MsgBox($MB_OK, "$vTaskProperties", $vTaskProperties)
    EndIf 
EndIf

;Test accessing the Trigger Object directly. 
ConsoleWrite("Triggers Count: " & $oTaskDefinition.Triggers.Count & @CRLF)

For $oTrigger In $oTaskDefinition.Triggers
    ConsoleWrite($oTrigger.Type & "  " & $oTrigger.Enabled & " " & $oTrigger.Id & "  " & $oTrigger.Repetition.Duration & "  " & $oTrigger.Repetition.Interval & "  " & _ 
    $oTrigger.Repetition.StopAtDurationEnd & "  " & $oTrigger.StartBoundary & "  " & $oTrigger.EndBoundary & "  " & $oTrigger.ExecutionTimeLimit & @CRLF)
Next

_TS_Close()

 

Adam

 

Link to comment
Share on other sites

Easy to fix ;)
Please replace in function _TS_TaskUpdate the following line

If Not IsObj($oTask) Or ObjName($oService) <> "IRegisteredTask" Then Return SetError(3302, 0, 0)

with

If Not IsObj($oTask) Or ObjName($oTask) <> "IRegisteredTask" Then Return SetError(3302, 0, 0)

 

A copy&paste error after I had tested the function and then tried to improve error checking :(

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I missed that when I looked at the code too.  I'm glad it was an easy change. 

Now there is another issue.  It is not updating the task.  I think it may be do to the task definition.  In my script above, I am updating a task definition object.  When I get the task object and give it to the function, it gets a local task definition object based on the non-updated task.  

 

Adam  

Link to comment
Share on other sites

I had to modify the function and add the TaskDefinition object as parameter. I still don't understand why it works this way but it seems to be the only way.
I have stripped down your example and it works:

#include "TaskScheduler.au3"

Global $sTaskName = "Test-Task"
Global $sTaskFolder = "\Test"
Global $sTask = $sTaskFolder & (($sTaskFolder = "\") ? ("") : ("\")) & $sTaskName
Global $oTask = Null
Global $oTaskDefinition = Null

; Connect to the Task Scheduler Service.
Global $oService = _TS_Open()

; Get Task Definition.
$oTask = _TS_TaskGet($oService, $sTask)
If @error Then MsgBox($MB_ICONERROR, "_TS_TaskGet", "Returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
MsgBox(0, "", "_TS_TaskGet successful", 2)
$oTaskDefinition = $oTask.Definition

; Check Triggers.
MsgBox(0, "", "Triggers Count before delete: " & $oTaskDefinition.Triggers.Count, 2)

; Delete all but the first trigger.
For $i = $oTaskDefinition.Triggers.Count To 2 Step -1
    _TS_TriggerDelete($oTaskDefinition, $i)
    If @error Then MsgBox($MB_ICONERROR, "_TS_TriggerDelete", "_TS_TriggerDelete returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
Next

; Update the task.  Does not work correctly.
$oTask = _TS_TaskUpdateEX($oService, $oTask, $oTaskDefinition)
If @error Then Exit MsgBox($MB_ICONERROR, "_TS_TaskUpdate", "_TS_TaskUpdate returned @error=" & @error & ", @extended=" & @extended & @CRLF & @CRLF & _TS_ErrorText(@error))
MsgBox(0, "", "Result of _TS_TaskUpdate: @error = " & @error & ", @extended = " & @extended, 2)

; Check Triggers.
MsgBox(0, "", "Triggers Count after delete: " & $oTaskDefinition.Triggers.Count, 2)

_TS_Close()

; #FUNCTION# ====================================================================================================================
; Name...........: _TS_TaskUpdate
; Description ...: Update a Task.
; Syntax.........: _TS_TaskRegister($oService, $oTask, $oTaskDefinition)
; Parameters ....: $oService        - Task Scheduler Service object as returned by _TS_Open
;                  $oTask           - Registered Task object
;                  $oTaskDefinition - TaskDefinition object of the Registered Task
; Return values .: Success - Task object
;                  Failure - Returns 0 and sets @error:
;                  |3301 - Parameter $oService is not an object or not an ITaskService object
;                  |3302 - Parameter $oTask is not an object or not an IRegisteredTask object
;                  |3303 - Error accessing $sFolder using _TS_FolderGet. @extended is set to the COM error
;                  |3304 - Error updating the Task. @extended is set to the COM error
; Author ........: water
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _TS_TaskUpdateEX($oService, $oTask, $oTaskDefinition)
    If Not IsObj($oService) Or ObjName($oService) <> "ITaskService" Then Return SetError(3301, 0, 0)
    If Not IsObj($oTask) Or ObjName($oTask) <> "IRegisteredTask" Then Return SetError(3302, 0, 0)
    Local $sTaskPath = $oTask.Path
    Local $iPos = StringInStr($sTaskPath, "\", 0, -1)
    Local $sFolder = ($iPos = 1) ? "\" : (StringLeft($sTaskPath, $iPos - 1))
    Local $sTask = StringMid($sTaskPath, $iPos + 1)
    Local $oFolder = _TS_FolderGet($oService, $sFolder)
    If @error Then Return SetError(3303, @error, 0)
    $oFolder.RegisterTaskDefinition($sTask, $oTaskDefinition, $TASK_UPDATE, "", "", $TASK_LOGON_NONE)
    If @error Then Return SetError(3304, @error, 0)
    Return $oTask
EndFunc   ;==>_TS_TaskUpdate

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...