#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include-once #include AutoItSetOption("MustDeclareVars", 1) ; #INDEX# ======================================================================================================================= ; Title .........: Microsoft Task Scheduler Function Library ; AutoIt Version : 3.3.14.5 ; UDF Version ...: 0.1.0.0 ; Language ......: English ; Description ...: A collection of functions to access and manipulate the Microsoft Tasks Scheduler Service ; Author(s) .....: allow2010, water ; Modified.......: 20190828 (YYYYMMDD) ; Contributors ..: ; Resources .....: https://docs.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-reference ; =============================================================================================================================== #Region #VARIABLES# ; #VARIABLES# =================================================================================================================== Global $__iTS_Debug = 0 ; Debug level. 0 = no debug information, 1 = Debug info to console, 2 = Debug info to MsgBox, 3 = Debug Info to File Global $__sTS_DebugFile = @ScriptDir & "\TaskScheduler_Debug.txt" ; Debug file when $__iTS_Debug is set to 3 Global $__oTS_Error ; COM Error handler ; =============================================================================================================================== #EndRegion #VARIABLES# #Region #CONSTANTS# ; #CONSTANTS# =================================================================================================================== ; TASK_STATE constants define the operational state of a task Global Const $TASK_STATE_UNKNOWN = 0 ; The state of the task is unknown Global Const $TASK_STATE_DISABLED = 1 ; The task is registered but is disabled and no instances of the task are queued or running. The task cannot be run until it is enabled Global Const $TASK_STATE_QUEUED = 2 ; Instances of the task are queued Global Const $TASK_STATE_READY = 3 ; The task is ready to be executed, but no instances are queued or running Global Const $TASK_STATE_RUNNING = 4 ; One or more instances of the task is running ; =============================================================================================================================== #EndRegion #CONSTANTS# ; #CURRENT# ===================================================================================================================== ;_TS_Open ;_TS_Close ;_TS_ErrorNotify ;_TS_FolderCreate ;_TS_FolderDelete ;_TS_FolderExists ;_TS_TaskGet ;_TS_TaskList ;_TS_TaskPropertiesGet ;_TS_VersionInfo ; =============================================================================================================================== ; #INTERNAL_USE_ONLY# =========================================================================================================== ;__TS_TaskPropertySet ;__TS_ErrorHandler ;__TS_ConvertDaysOfMonth ;__TS_ConvertDaysOfWeek ;__TS_ConvertMonthsofYear ;__TS_ConvertWeeksOfMonth ;__TS_WriteLine ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _TS_Open ; Description ...: Opens a connection to the Microsoft Task Scheduler Service. ; Syntax.........: _TS_Open([$sComputer[, $sUser[, $sDomain[, $sPassword]]]]) ; Parameters ....: $sComputer - Optional: The name of the computer that you want to connect to. If the serverName parameter is empty, then this method will execute on the local computer. ; $sUser - Optional: The user name that is used during the connection to the computer. If the user is not specified, then the current token is used. ; $sDomain - Optional: The domain of the user specified in the user parameter. ; $sPassword - Optional: The password that is used to connect to the computer. If the user name and password are not specified, then the current token is used. ; Return values .: Success - Object of the Task Scheduler Service ; Failure - Returns 0 and sets @error: ; |1 - Error creating the COM error handler. @extended is set to the error code returned by _TS_ErrorNotify ; |2 - Error creating the Task Scheduler Service. @extended is set to the COM error code ; |3 - Error connecting to the Task Scheduler Service. @extended is set to the COM error code: ; | 0x80070005 - Access is denied to connect to the Task Scheduler service. ; | 0x8007000e - The application does not have enough memory to complete the operation or ; | the user, password, or domain has at least one null and one non-null value. ; | 53 - This error is returned in the following situations: ; | The computer name specified in the serverName parameter does not exist. ; | When you are trying to connect to a Windows Server 2003 or Windows XP computer, and the remote computer does not have the ; | File and Printer Sharing firewall exception enabled or the Remote Registry service is not running. ; | When you are trying to connect to a Windows Vista computer, and the remote computer does not have the ; | Remote Scheduled Tasks Management firewall exception enabled and the File and Printer Sharing firewall exception enabled, or the Remote Registry service is not running. ; | 50 - The user, password, or domain parameters cannot be specified when connecting to a remote Windows XP or Windows Server 2003 computer from a Windows Vista computer. ; Author ........: water ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_Open($sComputer = Default, $sUser = Default, $sDomain = Default, $sPassword = Default) Local $oTS_Service _TS_ErrorNotify(4) If @error Then Return SetError(1, @error, 0) $oTS_Service = ObjCreate("Schedule.Service") If @error Then Return SetError(2, @error, 0) $oTS_Service.Connect($sComputer, $sUser, $sDomain, $sPassword) If @error Then Return SetError(3, @error, 0) Return $oTS_Service EndFunc ;==>_TS_Open ; #FUNCTION# ==================================================================================================================== ; Name ..........: _TS_Close ; Description ...: Closes a connection to the Microsoft Task Scheduler Service. ; Syntax.........: _TS_Close() ; Parameters ....: None ; Return values .: Success - 1 ; Author ........: water ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_Close() $__iTS_Debug = 0 $__sTS_DebugFile = @ScriptDir & "\TaskScheduler_Debug.txt" $__oTS_Error = 0 Return 1 EndFunc ;==>_TS_Close ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_ErrorNotify ; Description ...: Sets or queries the debug level. ; Syntax.........: _TS_ErrorNotify($iDebug[, $sDebugFile = @ScriptDir & "\TaskScheduler_Debug.txt"]) ; Parameters ....: $iDebug - Debug level. Allowed values are: ; |-1 - Return the current settings ; |0 - Disable debugging ; |1 - Enable debugging. Output the debug info to the console ; |2 - Enable Debugging. Output the debug info to a MsgBox ; |3 - Enable Debugging. Output the debug info to a file defined by $sDebugFile ; |4 - Enable Debugging. The COM errors will be handled (the script no longer crashes) without any output ; $sDebugFile - Optional: File to write the debugging info to if $iDebug = 3 (Default = @ScriptDir & "TaskScheduler_Debug.txt") ; Return values .: Success (for $iDebug => 0) - 1, sets @extended to: ; |0 - The COM error handler for this UDF was already active ; |1 - A COM error handler has been initialized for this UDF ; Success (for $iDebug = -1) - one based one-dimensional array with the following elements: ; |1 - Debug level. Value from 0 to 3. Check parameter $iDebug for details ; |2 - Debug file. File to write the debugging info to as defined by parameter $sDebugFile ; |3 - True if the COM error handler has been defined for this UDF. False if debugging is set off or a COM error handler was already defined ; Failure - 0, sets @error to: ; |1 - $iDebug is not an integer or < -1 or > 4 ; |2 - Installation of the custom error handler failed. @extended is set to the error code returned by ObjEvent ; |3 - COM error handler already set to another function ; Author ........: water ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_ErrorNotify($iDebug, $sDebugFile = "") If $sDebugFile = Default Then $sDebugFile = "" If Not IsInt($iDebug) Or $iDebug < -1 Or $iDebug > 4 Then Return SetError(1, 0, 0) If $sDebugFile = "" Then $sDebugFile = @ScriptDir & "\TaskScheduler_Debug.txt" Switch $iDebug Case -1 Local $avDebug[4] = [3] $avDebug[1] = $__iTS_Debug $avDebug[2] = $__sTS_DebugFile $avDebug[3] = IsObj($__oTS_Error) Return $avDebug Case 0 $__iTS_Debug = 0 $__sTS_DebugFile = "" $__oTS_Error = 0 Case Else $__iTS_Debug = $iDebug $__sTS_DebugFile = $sDebugFile ; A COM error handler will be initialized only if one does not exist If ObjEvent("AutoIt.Error") = "" Then $__oTS_Error = ObjEvent("AutoIt.Error", "__TS_ErrorHandler") ; Creates a custom error handler If @error <> 0 Then Return SetError(2, @error, 0) Return SetError(0, 1, 1) ElseIf ObjEvent("AutoIt.Error") = "__TS_ErrorHandler" Then Return SetError(0, 0, 1) ; COM error handler already set by a call to this function Else Return SetError(3, 0, 0) ; COM error handler already set to another function EndIf EndSwitch Return 1 EndFunc ;==>_TS_ErrorNotify ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_FolderCreate ; Description ...: Creates a task folder. ; Syntax.........: _TS_FolderCreate($oService, $sFolder) ; Parameters ....: $oService - Task Scheduler Service object as returned by _TS_Open ; $sFolder - The name that is used to identify the folder. It is created on the root folder ; If "FolderName\SubFolder1\SubFolder2" is specified, the entire folder tree will be created if the folders do not exist ; Return values .: Success - Object of the created task folder ; Failure - Returns 0 and sets @error: ; |1 - Error accessing the Taskfolder collection. @extended is set to the COM error code ; |2 - Specified $sFolder already exists ; |3 - Error creating the specified Taskfolder. @extended is set to the COM error code ; Author ........: water ; Modified.......: ; Remarks .......: $sFolder has always to start at the root folder (means you have to specify all the folders from the root down even when they already exist) ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_FolderCreate($oService, $sFolder) Local $oFolder = $oService.GetFolder("\") If @error Then Return SetError(1, @error, 0) $oService.GetFolder($sFolder) If @error = 0 Then Return SetError(2, 0, 0) Local $oFolderCreated = $oFolder.CreateFolder($sFolder) If @error Then Return SetError(3, @error, 0) Return $oFolderCreated EndFunc ;==>_TS_FolderCreate ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_FolderDelete ; Description ...: Deletes a task folder. ; Syntax.........: _TS_FolderDelete($oService, $sFolder) ; Parameters ....: $oService - Task Scheduler Service object as returned by _TS_Open ; $sFolder - The absolute path to the folder to be deleted ; e.g. \Folder-Level1\Folder-Level2. No trailing backslash allowed. ; Return values .: Success - 1 ; Failure - Returns 0 and sets @error: ; |1 - Error accessing the Taskfolder collection. @extended is set to the COM error code ; |2 - Specified $sFolder does not exist. @extended is set to the COM error code ; |3 - You can't delete a folder before all subfolders have been deleted. @extended is set to the COM error code ; |4 - Error deleting the specified Taskfolder. @extended is set to the COM error code ; Author ........: water ; Modified.......: ; Remarks .......: Before you can delete a folder you have to delete all subfolders ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_FolderDelete($oService, $sFolder) Local $oRoot = $oService.GetFolder("\") If @error Then Return SetError(1, @error, 0) Local $oFolder = $oService.GetFolder($sFolder) If @error Then Return SetError(2, @error, 0) Local $oSubFolders = $oFolder.GetFolders(0) If $oSubFolders.Count > 0 Then Return SetError(3, @error, 0) $oRoot.DeleteFolder($sFolder, 0) ; Parameter 2 is mandatory but not supported If @error Then Return SetError(4, @error, 0) Return 1 EndFunc ;==>_TS_FolderDelete ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_FolderExists ; Description ...: Checks if a task folder exists. ; Syntax.........: _TS_FolderExists($oService, $sFolder) ; Parameters ....: $oService - Task Scheduler Service object as returned by _TS_Open ; $sFolder - The absolute path to the folder to be checked ; e.g. \Folder-Level1\Folder-Level2. No trailing backslash allowed. ; Return values .: Success - 1 ; Failure - Returns 0 and sets @error: ; |1 - Error accessing the Taskfolder collection. @extended is set to the COM error code ; Author ........: water ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_FolderExists($oService, $sFolder) $oService.GetFolder($sFolder) If @error Then Return 0 Return 1 EndFunc ;==>_TS_FolderExists #cs ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_TaskCreate ; Description ...: Creates a new task. ; Syntax.........: _TS_TaskCreate($oService[, $sFolder = "\") ; Parameters ....: $oService - Task Scheduler Service object as returned by _TS_Open ; $sFolder - Task folder where the task is to be created ; Return values .: Success - Object of the created task ; Failure - Returns 0 and sets @error: ; |1 - Error accessing the specified Taskfolder. @extended is set to the COM error code ; Author ........: water ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_TaskCreate($oService, $sFolder, $sName, $aRegistration, $aSettings, $aPrincipal, $aTriggers, $aRepetition, $aActions) ; ==> $aTriggers umfasst folgende Objekte: BootTrigger, DailyTrigger, EventTrigger, idleTrigger, LogonTrigger, MonthlyDOWTrigger, Registrationtrigger, SessionStateChangeTrigger, TimeTrigger, Trigger, WeeklyTrigger ; Alle Arrays durchlaufen und jede Zeile mit Execute zuweisen. Kriege ich bei Fehler auch einen COM error? Ja ; Wrapper erstellen mit dem die Funktion "Einfache Aufgabe erstellen" nachgebildet wird. Local $oFolder, $oTask, $vResult, $oRegistrationInfo, $oSettings, $oPrincipal, $oTriggerCol, $oTrigger, $oTriggerRepetition, $oActionCol, $oAction, $aTemp $oFolder = $oService.GetFolder("\") ; ??? Task in anderen Foldern registrieren? $oTask = $oService.NewTask(0) ; RegistrationInfo $oRegistrationInfo = $oTask.RegistrationInfo() __TS_TaskPropertySet($oRegistrationInfo, $aRegistration) ; Settings $oSettings = $oTask.Settings() __TS_TaskPropertySet($oSettings, $aSettings) ; Principal $oPrincipal = $oTask.Principal() __TS_TaskPropertySet($oPrincipal, $aPrincipal) ; Trigger $oTriggerCol = $oTask.Triggers() $oTrigger = $oTriggerCol.Create($TriggerEvent) __TS_TaskPropertySet($oTrigger, $aTriggers) ; Repetition $oTriggerRepetition = $oTrigger.Repetition() __TS_TaskPropertySet($oTriggerRepetition, $aRepetition) ; Actions $oActionCol = $oTask.Actions() $oActionCol.Context() = @UserName $oAction = $oTask.Actions.Create(0) __TS_TaskPropertySet($oAction, $aActions) #cs ; call rootFolder.RegisterTaskDefinition(strTaskName, taskDefinition, FlagTaskCreate, , , LogonTypeInteractive) $oFolder.RegisterTaskDefinition($sName, $oTask, 6, $username, $password, $LogonType, "") #ce EndFunc ;==>_TS_TaskCreate #ce ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_TaskExists ; Description ...: Checks if a task exists. ; Syntax.........: _TS_TaskExists($oService, $sTaskPath) ; Parameters ....: $oService - Task Scheduler Service object as returned by _TS_Open ; $sTaskpath - Task path (folder plus task name) to process ; Return values .: Success - Object of the task ; Failure - Returns 0" and sets @error: ; |1 - Error accessing the specified Taskfolder. @extended is set to the COM error code ; |2 - Error retrieving the Tasks collection. @extended is set to the COM error code ; |3 - Task with the specified name could not be found in the task folder ; Author ........: water ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_TaskExists($oService, $sFolderPath) Local $sFolder, $oFolder, $sTaskName, $oTask, $iPos, $bHidden = 1 $iPos = StringInStr($sFolderPath, "\", 0, -1) $sTaskName = StringMid($sFolderPath, $iPos + 1) $sFolder = StringLeft($sFolderPath, $iPos - 1) $oFolder = $oService.GetFolder($sFolder) If @error Then Return SetError(1, @error, 0) Local $oTasks = $oFolder.GetTasks($bHidden) If @error Then Return SetError(2, @error, 0) For $oTask In $oTasks If $oTask.Name = $sTaskName Then Return 1 Next Return SetError(3, 0, 0) EndFunc ;==>_TS_TaskExists ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_TaskGet ; Description ...: Returns the object of a single tasks. ; Syntax.........: _TS_TaskGet($oService, $sTaskPath) ; Parameters ....: $oService - Task Scheduler Service object as returned by _TS_Open ; $sTaskpath - Task path (folder plus task name) to process ; Return values .: Success - Object of the task ; Failure - Returns 0" and sets @error: ; |1 - Error accessing the specified Taskfolder. @extended is set to the COM error code ; |2 - Error retrieving the Tasks collection. @extended is set to the COM error code ; |3 - Task with the specified name could not be found in the task folder ; Author ........: water ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_TaskGet($oService, $sFolderPath) Local $sFolder, $oFolder, $sTaskName, $oTask, $iPos, $bHidden = 1 $iPos = StringInStr($sFolderPath, "\", 0, -1) $sTaskName = StringMid($sFolderPath, $iPos + 1) $sFolder = StringLeft($sFolderPath, $iPos - 1) $oFolder = $oService.GetFolder($sFolder) If @error Then Return SetError(1, @error, 0) Local $oTasks = $oFolder.GetTasks($bHidden) If @error Then Return SetError(2, @error, 0) For $oTask In $oTasks If $oTask.Name = $sTaskName Then Return $oTask Next Return SetError(3, 0, 0) EndFunc ;==>_TS_TaskGet ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_TaskList ; Description ...: Returns a list of all tasks in a given taskfolder. ; Syntax.........: _TS_TaskList($oService[, $sFolder = "\"[, $iHidden = 0]]) ; Parameters ....: $oService - Task Scheduler Service object as returned by _TS_Open ; $sFolder - Optional: Task folder to process (default = "\" means root folder) ; $iHidden - Optional: Returns hidden tasks when set to 1 (default = 0) ; Return values .: Success - two-dimensional zero based array with the following information: ; |0 - Object of the task ; |1 - Name of the task ; |3 - Path to where the registered task is stored ; |3 - State of the task as defined in the TASK_STATE constants ; |4 - Boolean value that indicates if the task is enabled ; Failure - Returns "" and sets @error: ; |1 - Error accessing the specified Taskfolder. @extended is set to the COM error code ; |2 - Error retrieving the Tasks collection. @extended is set to the COM error code ; Author ........: water ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_TaskList($oService, $sFolder = "\", $bHidden = 0) If $sFolder = Default Then $sFolder = "\" If $bHidden = Default Then $bHidden = 0 Local $oFolder = $oService.GetFolder($sFolder) If @error Then Return SetError(1, @error, "") Local $oTasks = $oFolder.GetTasks($bHidden) If @error Then Return SetError(2, @error, "") Local $iTaskCount = $oTasks.Count, $aTasks[$iTaskCount][5] For $i = 1 To $iTaskCount $aTasks[$i - 1][0] = $oTasks($i) $aTasks[$i - 1][1] = $oTasks($i).Name $aTasks[$i - 1][2] = $oTasks($i).Path $aTasks[$i - 1][3] = $oTasks($i).State $aTasks[$i - 1][4] = $oTasks($i).Enabled Next Return $aTasks EndFunc ;==>_TS_TaskList ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_TaskPropertiesGet ; Description ...: List all properties of a task. ; Syntax.........: _TS_TaskPropertiesGet($oService, $sTask[, $iFormat = 1]) ; Parameters ....: $oService - Task Scheduler Service object as returned by _TS_Open ; $sTask - The name of the task to process ; $iFormat - Format of the output. Can be one of the following values: ; |1 - User friendly format (default) ; |2 - Format you can use as input to _TS_TaskCreate ; Return values .: Success - two-dimensional zero based array with the following information: ; | ; Failure - Returns "" and sets @error: ; |1 - Error returned by _TS_TaskGet. @extended is set to the COM error code. Most probably the task could not be found ; Author ........: water ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_TaskPropertiesGet($oTaskService, $sTask, $iFormat) Local $oFSO, $oTextFile #forceref $iFormat, $oFSO, $oTextFile Local $oTask, $sTaskState Local $oTaskDefinition, $cActions, $oTaskAction, $cAttachments, $sAttachment Local $cHeaderFields, $oHeaderPair Local $oPrincipal, $oRegistrationInfo, $oTaskSettings, $oIdleSettings, $oTaskNetworkSettings Local $cTaskTriggers, $oTaskTrigger, $oTaskRepitition Local $sTaskProperties, $aTaskProperties $oTask = _TS_TaskGet($oTaskService, $sTask) If @error Then Return SetError(1, @error, "") ; http://msdn.microsoft.com/en-us/library/windows/desktop/aa382079%28v=vs.85%29.aspx With $oTask __TS_WriteLine($sTaskProperties, "Task Name: " & .Name) __TS_WriteLine($sTaskProperties, " -Enabled:" & .Enabled) __TS_WriteLine($sTaskProperties, " -LastRunTime:" & .LastRunTime) __TS_WriteLine($sTaskProperties, " -LastTaskResult:" & .LastTaskResult) __TS_WriteLine($sTaskProperties, " -NextRunTime:" & .NextRunTime) __TS_WriteLine($sTaskProperties, " -NumberOfMissedRuns:" & .NumberOfMissedRuns) __TS_WriteLine($sTaskProperties, " -Path:" & .Path) Switch (.State) Case "0" $sTaskState = "Unknown" Case "1" $sTaskState = "Disabled" Case "2" $sTaskState = "Queued" Case "3" $sTaskState = "Ready" Case "4" $sTaskState = "Running" EndSwitch __TS_WriteLine($sTaskProperties, " -Task State: " & .State & "=" & $sTaskState) ; http://msdn.microsoft.com/en-us/library/windows/desktop/aa382542%28v=vs.85%29.aspx $oTaskDefinition = .Definition With $oTaskDefinition __TS_WriteLine($sTaskProperties, " -TASK DEFINITION") __TS_WriteLine($sTaskProperties, " -Data:" & .Data) ;http://msdn.microsoft.com/en-us/library/windows/desktop/aa446803%28v=vs.85%29.aspx $cActions = $oTaskDefinition.Actions For $oTaskAction In $cActions With $oTaskAction __TS_WriteLine($sTaskProperties, " -ACTIONS") __TS_WriteLine($sTaskProperties, " -ID:" & .Id) Switch (.Type) Case 0 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & "= Execute / Command Line Operation") __TS_WriteLine($sTaskProperties, " -Arguments :" & .Arguments) __TS_WriteLine($sTaskProperties, " -Path:" & .Path) __TS_WriteLine($sTaskProperties, " -WorkingDirectory:" & .WorkingDirectory) Case 5 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & "= Handler") __TS_WriteLine($sTaskProperties, " -ClassId:" & .ClassId) __TS_WriteLine($sTaskProperties, " -Data:" & .Data) Case 6 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & "= Email Message") __TS_WriteLine($sTaskProperties, " -From:" & .From) __TS_WriteLine($sTaskProperties, " -ReplyTo:" & .ReplyTo) __TS_WriteLine($sTaskProperties, " -To:" & .To) __TS_WriteLine($sTaskProperties, " -Cc:" & .Cc) __TS_WriteLine($sTaskProperties, " -Bcc:" & .Bcc) __TS_WriteLine($sTaskProperties, " -Subject:" & .Subject) __TS_WriteLine($sTaskProperties, " -Body:" & .Body) __TS_WriteLine($sTaskProperties, " -Server:" & .Server) $cAttachments = .Attachments For $sAttachment In $cAttachments __TS_WriteLine($sTaskProperties, " -Attachment:" & $sAttachment) Next ;sAttachment $cHeaderFields = .HeaderFields For $oHeaderPair In $cHeaderFields __TS_WriteLine($sTaskProperties, " -HeaderName:" & $oHeaderPair.Name & " | Value:" & $oHeaderPair.Value) Next ;objHeaderPair Case 7 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & "=Message Box") __TS_WriteLine($sTaskProperties, " -Title:" & .Title) __TS_WriteLine($sTaskProperties, " -MessageBody:" & .MessageBody) EndSwitch EndWith ;objTaskAction Next ; objTaskAction ; http://msdn.microsoft.com/en-us/library/windows/desktop/aa382071%28v=vs.85%29.aspx $oPrincipal = .Principal __TS_WriteLine($sTaskProperties, " -PRINCIPAL") With $oPrincipal __TS_WriteLine($sTaskProperties, " -ID:" & .Id) __TS_WriteLine($sTaskProperties, " -DisplayName:" & .DisplayName) __TS_WriteLine($sTaskProperties, " -GroupId:" & .GroupId) __TS_WriteLine($sTaskProperties, " -ID:" & .Id) Switch (.LogonType) Case 0 __TS_WriteLine($sTaskProperties, " -LogonType:" & .LogonType & " = None") Case 1 __TS_WriteLine($sTaskProperties, " -LogonType:" & .LogonType & " = Password") Case 2 __TS_WriteLine($sTaskProperties, " -LogonType:" & .LogonType & " = Service 4 Users") Case 3 __TS_WriteLine($sTaskProperties, " -LogonType:" & .LogonType & " = Interactive (User Must be logged in)") Case 4 __TS_WriteLine($sTaskProperties, " -LogonType:" & .LogonType & " = Group") Case 5 __TS_WriteLine($sTaskProperties, " -LogonType:" & .LogonType & " = Local $Service/System or Network Service") Case 6 __TS_WriteLine($sTaskProperties, " -LogonType:" & .LogonType & " = Interactive Token then Try Password") EndSwitch Switch (.RunLevel) Case 0 __TS_WriteLine($sTaskProperties, " -RunLevel:" & .RunLevel & " = Least Privileges (LUA)") Case 1 __TS_WriteLine($sTaskProperties, " -RunLevel:" & .RunLevel & " = Highest Privileges") EndSwitch __TS_WriteLine($sTaskProperties, " -UserId:" & .UserId) EndWith ;objPrincipal ;http://msdn.microsoft.com/en-us/library/windows/desktop/aa382100%28v=vs.85%29.aspx $oRegistrationInfo = .RegistrationInfo __TS_WriteLine($sTaskProperties, " -REGISTRATION INFO") With $oRegistrationInfo __TS_WriteLine($sTaskProperties, " -Author:" & .Author) __TS_WriteLine($sTaskProperties, " -Date:" & .Date) __TS_WriteLine($sTaskProperties, " -Description:" & .Description) __TS_WriteLine($sTaskProperties, " -Date:" & .Date) __TS_WriteLine($sTaskProperties, " -Documentation :" & .Documentation) __TS_WriteLine($sTaskProperties, " -SecurityDescriptor:" & .SecurityDescriptor) __TS_WriteLine($sTaskProperties, " -Source:" & .Source) __TS_WriteLine($sTaskProperties, " -URI:" & .URI) __TS_WriteLine($sTaskProperties, " -Version:" & .Version) EndWith ;objRegistrationInfo ;http://msdn.microsoft.com/en-us/library/windows/desktop/aa383480%28v=vs.85%29.aspx $oTaskSettings = .Settings __TS_WriteLine($sTaskProperties, " -TASK SETTINGS") With $oTaskSettings __TS_WriteLine($sTaskProperties, " -AllowDemandStart:" & .AllowDemandStart) __TS_WriteLine($sTaskProperties, " -AllowHardTerminate:" & .AllowHardTerminate) Switch (.Compatibility) Case 0 __TS_WriteLine($sTaskProperties, " -Compatibility:" & .Compatibility & " = compatible with the AT command") Case 1 __TS_WriteLine($sTaskProperties, " -Compatibility:" & .Compatibility & " = compatible with Task Scheduler 1.0") Case 2 __TS_WriteLine($sTaskProperties, " -Compatibility:" & .Compatibility & " = compatible with Task Scheduler 2.0 (Windows Vista / Windows 2008") Case 3 ; Not Documented __TS_WriteLine($sTaskProperties, " -Compatibility:" & .Compatibility & " = compatible with Task Scheduler 2.0 (Windows 7 / Windows 2008 R2") EndSwitch __TS_WriteLine($sTaskProperties, " -DeleteExpiredTaskAfter:" & .DeleteExpiredTaskAfter) __TS_WriteLine($sTaskProperties, " -DisallowStartIfOnBatteries:" & .DisallowStartIfOnBatteries) __TS_WriteLine($sTaskProperties, " -Enabled:" & .Enabled) __TS_WriteLine($sTaskProperties, " -ExecutionTimeLimit:" & .ExecutionTimeLimit) __TS_WriteLine($sTaskProperties, " -Hidden:" & .Hidden) Switch (.MultipleInstances) Case 0 __TS_WriteLine($sTaskProperties, " -MultipleInstances:" & .MultipleInstances & " = Run in Parallel") Case 1 __TS_WriteLine($sTaskProperties, " -MultipleInstances:" & .MultipleInstances & " = Add to Queue") Case 2 __TS_WriteLine($sTaskProperties, " -MultipleInstances:" & .MultipleInstances & " = Ignore New") EndSwitch __TS_WriteLine($sTaskProperties, " -Priority:" & .Priority & " (0=High / 10= Low)") __TS_WriteLine($sTaskProperties, " -RestartCount:" & .RestartCount) __TS_WriteLine($sTaskProperties, " -RestartInterval:" & .RestartInterval) __TS_WriteLine($sTaskProperties, " -RunOnlyIfIdle:" & .RunOnlyIfIdle) __TS_WriteLine($sTaskProperties, " -RunOnlyIfNetworkAvailable:" & .RunOnlyIfNetworkAvailable) __TS_WriteLine($sTaskProperties, " -StartWhenAvailable:" & .StartWhenAvailable) __TS_WriteLine($sTaskProperties, " -StopIfGoingOnBatteries:" & .StopIfGoingOnBatteries) __TS_WriteLine($sTaskProperties, " -WakeToRun:" & .WakeToRun) ;http://msdn.microsoft.com/en-us/library/windows/desktop/aa380669%28v=vs.85%29.aspx $oIdleSettings = .IdleSettings __TS_WriteLine($sTaskProperties, " -IDLE SETTINGS") With $oIdleSettings __TS_WriteLine($sTaskProperties, " -IdleDuration:" & .IdleDuration) __TS_WriteLine($sTaskProperties, " -RestartOnIdle:" & .RestartOnIdle) __TS_WriteLine($sTaskProperties, " -StopOnIdleEnd:" & .StopOnIdleEnd) __TS_WriteLine($sTaskProperties, " -WaitTimeout:" & .WaitTimeout) EndWith ;objIdleSettings ;http://msdn.microsoft.com/en-us/library/windows/desktop/aa382067%28v=vs.85%29.aspx $oTaskNetworkSettings = .NetworkSettings __TS_WriteLine($sTaskProperties, " -NETWORK SETTINGS") With $oTaskNetworkSettings __TS_WriteLine($sTaskProperties, " -ID:" & .Id) __TS_WriteLine($sTaskProperties, " -Name:" & .Name) EndWith ;objTaskNetworkSettings EndWith ;objTaskSettings ;http://msdn.microsoft.com/en-us/library/windows/desktop/aa383868%28v=vs.85%29.aspx $cTaskTriggers = .Triggers For $oTaskTrigger In $cTaskTriggers __TS_WriteLine($sTaskProperties, " -TRIGGER") With $oTaskTrigger __TS_WriteLine($sTaskProperties, " -Enabled:" & .Enabled) __TS_WriteLine($sTaskProperties, " -Id:" & .Id) __TS_WriteLine($sTaskProperties, " -StartBoundary:" & .StartBoundary) __TS_WriteLine($sTaskProperties, " -EndBoundary:" & .EndBoundary) __TS_WriteLine($sTaskProperties, " -ExecutionTimeLimit:" & .ExecutionTimeLimit) Switch (.Type) Case 0 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = Event") __TS_WriteLine($sTaskProperties, " -Delay:" & .Delay) __TS_WriteLine($sTaskProperties, " -Subscription :" & .Subscription) Case 1 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = Time") Case 2 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = Daily") __TS_WriteLine($sTaskProperties, " -DaysInterval:" & .DaysInterval) Case 3 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = Weekly") __TS_WriteLine($sTaskProperties, " -WeeksInterval:" & .WeeksInterval) __TS_WriteLine($sTaskProperties, " -DaysOfWeek:" & .DaysOfWeek & "=" & __TS_ConvertDaysOfWeek(.DaysOfWeek)) Case 4 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = Monthly") __TS_WriteLine($sTaskProperties, " -DaysOfMonth:" & .DaysOfMonth & "=" & __TS_ConvertDaysOfMonth(.DaysOfMonth)) __TS_WriteLine($sTaskProperties, " -MonthsOfYear:" & .MonthsOfYear & "=" & __TS_ConvertMonthsofYear(.MonthsOfYear)) __TS_WriteLine($sTaskProperties, " -RandomDelay:" & .RandomDelay) __TS_WriteLine($sTaskProperties, " -RunOnLastDayOfMonth :" & .RunOnLastDayOfMonth) Case 5 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = Monthly on Specific Day") __TS_WriteLine($sTaskProperties, " -DaysOfWeek:" & .DaysOfWeek & "=" & __TS_ConvertDaysOfWeek(.DaysOfWeek)) __TS_WriteLine($sTaskProperties, " -MonthsOfYear:" & .MonthsOfYear & "=" & __TS_ConvertMonthsofYear(.MonthsOfYear)) __TS_WriteLine($sTaskProperties, " -RandomDelay:" & .RandomDelay) __TS_WriteLine($sTaskProperties, " -RunOnLastWeekOfMonth :" & .RunOnLastWeekOfMonth) __TS_WriteLine($sTaskProperties, " -WeeksOfMonth:" & .WeeksOfMonth & "=" & __TS_ConvertWeeksOfMonth(.WeeksOfMonth)) Case 6 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = When Computer is idle") Case 7 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = When Task is registered") __TS_WriteLine($sTaskProperties, " -Delay:" & .Delay) Case 8 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = Boot") __TS_WriteLine($sTaskProperties, " -Delay:" & .Delay) Case 9 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = Logon") __TS_WriteLine($sTaskProperties, " -Delay:" & .Delay) __TS_WriteLine($sTaskProperties, " -UserId:" & .UserId) Case 11 __TS_WriteLine($sTaskProperties, " -Type:" & .Type & " = Session State Change") Switch (.StateChange) Case 0 __TS_WriteLine($sTaskProperties, " -LogonType:" & .StateChange & " = None") Case 1 __TS_WriteLine($sTaskProperties, " -LogonType:" & .StateChange & " = User Session Connect to Local $ Computer") Case 2 __TS_WriteLine($sTaskProperties, " -LogonType:" & .StateChange & " = User Session Disconnect from Local $Computer") Case 3 __TS_WriteLine($sTaskProperties, " -LogonType:" & .StateChange & " = User Session Connect to Remote Computer") Case 4 __TS_WriteLine($sTaskProperties, " -LogonType:" & .StateChange & " = User Session Disconnect from Remote Computer") Case 7 __TS_WriteLine($sTaskProperties, " -LogonType:" & .StateChange & " = On Workstation Lock") Case 8 __TS_WriteLine($sTaskProperties, " -LogonType:" & .StateChange & " = On Workstation Unlock") EndSwitch __TS_WriteLine($sTaskProperties, " -Delay:" & .Delay) __TS_WriteLine($sTaskProperties, " -UserId:" & .UserId) EndSwitch $oTaskRepitition = .Repetition __TS_WriteLine($sTaskProperties, " -REPITITION") With $oTaskRepitition __TS_WriteLine($sTaskProperties, " -Duration:" & .Duration) __TS_WriteLine($sTaskProperties, " -Interval:" & .Interval) __TS_WriteLine($sTaskProperties, " -StopAtDurationEnd:" & .StopAtDurationEnd) EndWith ;objTaskRepitition EndWith ;objTaskTrigger Next ;objTaskTrigger EndWith ;objTaskDefinition __TS_WriteLine($sTaskProperties, " -XML:" & .XML) EndWith ;objTask $aTaskProperties = StringSplit($sTaskProperties, @CRLF, $STR_ENTIRESPLIT + $STR_NOCOUNT) Return $aTaskProperties EndFunc ;==>_TS_TaskPropertiesGet ; #FUNCTION# ==================================================================================================================== ; Name...........: _TS_VersionInfo ; Description ...: Returns an array of information about the UDF. ; Syntax.........: _TS_VersionInfo() ; Parameters ....: None ; Return values .: Success - one-dimensional one based array with the following information: ; |1 - Release Type (T=Test or V=Production) ; |2 - Major Version ; |3 - Minor Version ; |4 - Sub Version ; |5 - Release Date (YYYYMMDD) ; |6 - AutoIt version required ; |7 - List of authors separated by "," ; |8 - List of contributors separated by "," ; Author ........: water ; Modified.......: ; Remarks .......: Based on function _IE_VersionInfo written bei Dale Hohm ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _TS_VersionInfo() Local $aVersionInfo[9] = [8, "V", 0, 1, 0.0, "20190828", "3.3.14.5", "allow2010, water", ""] Return $aVersionInfo EndFunc ;==>_TS_VersionInfo Func __TS_TaskPropertySet(ByRef $oObject, $aArray) Local $aTemp, $vResult #forceref $oObject, $vResult If IsArray($aArray) Then For $i = 0 To UBound($aArray) - 1 $aTemp = StringSplit($aArray[$i], "=", $STR_NOCOUNT) If @error Then ContinueLoop $vResult = Execute("$oObject." & $aTemp[0] & "=" & $aTemp[1]) If @error Then Return SetError(1, @error, 0) Next EndIf EndFunc ;==>__TS_TaskPropertySet ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name ..........: __TS_ErrorHandler ; Description ...: Called if an ObjEvent error occurs. ; Syntax.........: __TS_ErrorHandler() ; Parameters ....: None ; Return values .: @error is set to the COM error by AutoIt ; Author ........: water ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __TS_ErrorHandler() Local $bHexNumber = Hex($__oTS_Error.number, 8) Local $aVersionInfo = _TS_VersionInfo() Local $sError = "COM Error Encountered in " & @ScriptName & @CRLF & _ "TS UDF version = " & $aVersionInfo[2] & "." & $aVersionInfo[3] & "." & $aVersionInfo[4] & @CRLF & _ "@AutoItVersion = " & @AutoItVersion & @CRLF & _ "@AutoItX64 = " & @AutoItX64 & @CRLF & _ "@Compiled = " & @Compiled & @CRLF & _ "@OSArch = " & @OSArch & @CRLF & _ "@OSVersion = " & @OSVersion & @CRLF & _ "Scriptline = " & $__oTS_Error.scriptline & @CRLF & _ "NumberHex = " & $bHexNumber & @CRLF & _ "Number = " & $__oTS_Error.number & @CRLF & _ "WinDescription = " & StringStripWS($__oTS_Error.WinDescription, 2) & @CRLF & _ "Description = " & StringStripWS($__oTS_Error.Description, 2) & @CRLF & _ "Source = " & $__oTS_Error.Source & @CRLF & _ "HelpFile = " & $__oTS_Error.HelpFile & @CRLF & _ "HelpContext = " & $__oTS_Error.HelpContext & @CRLF & _ "LastDllError = " & $__oTS_Error.LastDllError If $__iTS_Debug > 0 Then If $__iTS_Debug = 1 Then ConsoleWrite($sError & @CRLF & "========================================================" & @CRLF) If $__iTS_Debug = 2 Then MsgBox(64, "TS UDF - Debug Info", $sError) If $__iTS_Debug = 3 Then FileWrite($__sTS_DebugFile, @YEAR & "." & @MON & "." & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " " & @CRLF & _ "-------------------" & @CRLF & $sError & @CRLF & "========================================================" & @CRLF) EndIf EndFunc ;==>__TS_ErrorHandler Func __TS_ConvertDaysOfMonth($BitValue) Local $sMsg = "" If BitAND($BitValue, 0x01) Then $sMsg = "1" If BitAND($BitValue, 0x02) Then $sMsg = $sMsg & "2" If BitAND($BitValue, 0x04) Then $sMsg = $sMsg & "3" If BitAND($BitValue, 0x08) Then $sMsg = $sMsg & "4" If BitAND($BitValue, 0x10) Then $sMsg = $sMsg & "5" If BitAND($BitValue, 0x20) Then $sMsg = $sMsg & "6" If BitAND($BitValue, 0x40) Then $sMsg = $sMsg & "7" If BitAND($BitValue, 0x80) Then $sMsg = $sMsg & "8" If BitAND($BitValue, 0x100) Then $sMsg = $sMsg & "9" If BitAND($BitValue, 0x200) Then $sMsg = $sMsg & "10" If BitAND($BitValue, 0x400) Then $sMsg = $sMsg & "11" If BitAND($BitValue, 0x800) Then $sMsg = $sMsg & "12" If BitAND($BitValue, 0x1000) Then $sMsg = $sMsg & "13" If BitAND($BitValue, 0x2000) Then $sMsg = $sMsg & "14" If BitAND($BitValue, 0x4000) Then $sMsg = $sMsg & "15" If BitAND($BitValue, 0x8000) Then $sMsg = $sMsg & "16" If BitAND($BitValue, 0x10000) Then $sMsg = $sMsg & "17" If BitAND($BitValue, 0x20000) Then $sMsg = $sMsg & "18" If BitAND($BitValue, 0x40000) Then $sMsg = $sMsg & "19" If BitAND($BitValue, 0x80000) Then $sMsg = $sMsg & "20" If BitAND($BitValue, 0x100000) Then $sMsg = $sMsg & "21" If BitAND($BitValue, 0x200000) Then $sMsg = $sMsg & "22" If BitAND($BitValue, 0x400000) Then $sMsg = $sMsg & "23" If BitAND($BitValue, 0x800000) Then $sMsg = $sMsg & "24" If BitAND($BitValue, 0x1000000) Then $sMsg = $sMsg & "25" If BitAND($BitValue, 0x2000000) Then $sMsg = $sMsg & "26" If BitAND($BitValue, 0x4000000) Then $sMsg = $sMsg & "27" If BitAND($BitValue, 0x8000000) Then $sMsg = $sMsg & "28" If BitAND($BitValue, 0x10000000) Then $sMsg = $sMsg & "29" If BitAND($BitValue, 0x20000000) Then $sMsg = $sMsg & "30" If BitAND($BitValue, 0x40000000) Then $sMsg = $sMsg & "31" If BitAND($BitValue, 0x80000000) Then $sMsg = $sMsg & "LAST" If StringLeft($sMsg, 2) = ", " Then $sMsg = StringMid($sMsg, 3) ; Remove leading ", " Return $sMsg EndFunc ;==>__TS_ConvertDaysOfMonth Func __TS_ConvertDaysOfWeek($BitValue) Local $sMsg = "" If BitAND($BitValue, 1) Then $sMsg = "Sunday" If BitAND($BitValue, 2) Then $sMsg = $sMsg & ", Monday" If BitAND($BitValue, 4) Then $sMsg = $sMsg & ", Tuesday" If BitAND($BitValue, 8) Then $sMsg = $sMsg & ", Wednesday" If BitAND($BitValue, 16) Then $sMsg = $sMsg & ", Thursday" If BitAND($BitValue, 32) Then $sMsg = $sMsg & ", Friday" If BitAND($BitValue, 64) Then $sMsg = $sMsg & ", Saturday" If StringLeft($sMsg, 2) = ", " Then $sMsg = StringMid($sMsg, 3) ; Remove leading ", " Return $sMsg EndFunc ;==>__TS_ConvertDaysOfWeek Func __TS_ConvertMonthsofYear($BitValue) Local $sMsg = "" If BitAND($BitValue, 1) Then $sMsg = "January" If BitAND($BitValue, 2) Then $sMsg = $sMsg & ", February" If BitAND($BitValue, 4) Then $sMsg = $sMsg & ", March" If BitAND($BitValue, 8) Then $sMsg = $sMsg & ", April" If BitAND($BitValue, 16) Then $sMsg = $sMsg & ", May" If BitAND($BitValue, 32) Then $sMsg = $sMsg & ", June" If BitAND($BitValue, 64) Then $sMsg = $sMsg & ", July" If BitAND($BitValue, 128) Then $sMsg = $sMsg & ", August" If BitAND($BitValue, 256) Then $sMsg = $sMsg & ", September" If BitAND($BitValue, 512) Then $sMsg = $sMsg & ", October" If BitAND($BitValue, 1024) Then $sMsg = $sMsg & ", November" If BitAND($BitValue, 2048) Then $sMsg = $sMsg & ", December" If StringLeft($sMsg, 2) = ", " Then $sMsg = StringMid($sMsg, 3) ; Remove leading ", " Return $sMsg EndFunc ;==>__TS_ConvertMonthsofYear Func __TS_ConvertWeeksOfMonth($BitValue) Local $sMsg = "" If BitAND($BitValue, 1) Then $sMsg = "First" If BitAND($BitValue, 2) Then $sMsg = $sMsg & ", Second" If BitAND($BitValue, 4) Then $sMsg = $sMsg & ", Third" If BitAND($BitValue, 8) Then $sMsg = $sMsg & ", Fourth" If StringLeft($sMsg, 2) = ", " Then $sMsg = StringMid($sMsg, 3) ; Remove leading ", " Return $sMsg EndFunc ;==>__TS_ConvertWeeksOfMonth Func __TS_WriteLine(ByRef $sTaskProperties, $sValue) If $sTaskProperties = "" Then $sTaskProperties = $sValue Else $sTaskProperties = $sTaskProperties & @CRLF & $sValue EndIf Return EndFunc ;==>__TS_WriteLine ;>>>>>>>>>>>>>>>>>>>>>>>>> Bis hierher aktualisiert #cs ;================================================================================== ; Function: _TaskExists($taskname, $folder = "\") ; Description: check if a Task exists ; Return Value(s): On Success - Return 1, @ERROR = 0 ; On Failure - Sets @ERROR = 1, Return 0 ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!) ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== Func _TaskExists($taskname, $folder = "\") ;check if a Task exists If Not _TaskIsValidPlatform() Then Return SetError(2, 2, 0) Local $oService, $oFolder ; $oMyError = ObjEvent("AutoIt.Error", "MyTaskErrFunc") ; Initialize a COM error handler $oService = ObjCreate("Schedule.Service") $oService.Connect() $oFolder = $oService.GetFolder($folder) If IsObj($oFolder) Then $oTask = $oFolder.GetTask($taskname) If IsObj($oTask) Then $retval = 1 Else $retval = 0 EndIf Else $retval = 0 EndIf $oService = 0 If $ComErrorFound Then $ComErrorFound = 0 SetError(1) Return $retval Else Return $retval EndIf EndFunc ;==>_TaskExists ;================================================================================== ; Function: _TaskStop($taskname, $folder = "\") ; Description: stop a task ; Return Value(s): On Success - Return 1, @ERROR = 0 ; On Failure - Sets @ERROR = 1, Return 0 ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!) ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== Func _TaskStop($taskname, $folder = "\") If Not _TaskIsValidPlatform() Then Return SetError(2, 2, 0) Local $oService, $oFolder ; $oMyError = ObjEvent("AutoIt.Error", "MyTaskErrFunc") ; Initialize a COM error handler $oService = ObjCreate("Schedule.Service") $oService.Connect() $oFolder = $oService.GetFolder($folder) $temperror = @error If IsObj($oFolder) Then $oTask = $oFolder.GetTask($taskname) $instcount = 1 If IsObj($oTask) Then $oTask.Stop(0) For $counter = 0 To 500 ;when the task is restarted (fast) after stoping it, it will look like a failure to stop it Sleep(100) $instances = $oTask.GetInstances(0) If IsObj($instances) Then If $instances.Count() = 0 Then $instcount = 0 ExitLoop EndIf EndIf Next If $instcount = 0 Then $retval = 1 Else $retval = 0 EndIf Else $retval = 0 EndIf Else $retval = 0 EndIf $oService = 0 If $ComErrorFound Then $ComErrorFound = 0 SetError(1) Return $retval Else Return $retval EndIf EndFunc ;==>_TaskStop ;================================================================================== ; Function: _TaskEnable($taskname, $folder = "\") ; Description: Enables a task ; Return Value(s): On Success - Return 1, @ERROR = 0 ; On Failure - Sets @ERROR = 1, Return 0 ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!) ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== Func _TaskEnable($taskname, $folder = "") ; enable a Task If Not _TaskIsValidPlatform() Then Return SetError(2, 2, 0) Local $oService, $oFolder ; $oMyError = ObjEvent("AutoIt.Error", "MyTaskErrFunc") ; Initialize a COM error handler $oService = ObjCreate("Schedule.Service") $oService.Connect() $oFolder = $oService.GetFolder($folder) If IsObj($oFolder) Then $oTask = $oFolder.GetTask($taskname) $instcount = 0 If IsObj($oTask) Then $oTask.Enabled = True $retval = 1 Else $retval = 0 EndIf Else $retval = 0 EndIf $oService = 0 If $ComErrorFound Then $ComErrorFound = 0 SetError(1) Return $retval Else Return $retval EndIf EndFunc ;==>_TaskEnable ;================================================================================== ; Function: _TaskDisable($taskname, $folder = "\") ; Description: Disables a task ; Return Value(s): On Success - Return 1, @ERROR = 0 ; On Failure - Sets @ERROR = 1, Return 0 ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!) ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== Func _TaskDisable($taskname, $folder = "") ; disable a Task If Not _TaskIsValidPlatform() Then Return SetError(2, 2, 0) Local $oService, $oFolder ; $oMyError = ObjEvent("AutoIt.Error", "MyTaskErrFunc") ; Initialize a COM error handler $oService = ObjCreate("Schedule.Service") $oService.Connect() $oFolder = $oService.GetFolder($folder) If IsObj($oFolder) Then $oTask = $oFolder.GetTask($taskname) $instcount = 0 If IsObj($oTask) Then $oTask.Enabled = False $retval = 1 Else $retval = 0 EndIf Else $retval = 0 EndIf $oService = 0 If $ComErrorFound Then $ComErrorFound = 0 SetError(1) Return $retval Else Return $retval EndIf EndFunc ;==>_TaskDisable ;================================================================================== ; Function: _TaskIsEnabled($taskname, $folder = "\") ; Description: check if a task is anabled ; Return Value(s): On Success - Return 1 or 0, @ERROR = 0 ; On Failure - Sets @ERROR = 1, Return 0 ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!) ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== Func _TaskIsEnabled($taskname, $folder = "") ;check if a Task is enabled If Not _TaskIsValidPlatform() Then Return SetError(2, 2, 0) Local $oService, $oFolder ; $oMyError = ObjEvent("AutoIt.Error", "MyTaskErrFunc") ; Initialize a COM error handler $oService = ObjCreate("Schedule.Service") $oService.Connect() $oFolder = $oService.GetFolder($folder) If IsObj($oFolder) Then $oTask = $oFolder.GetTask($taskname) $instcount = 0 If IsObj($oTask) Then $retval = $oTask.Enabled Else $retval = 0 EndIf Else $retval = 0 EndIf $oService = 0 If $ComErrorFound Then $ComErrorFound = 0 SetError(1) Return $retval Else Return $retval EndIf EndFunc ;==>_TaskIsEnabled ;================================================================================== ; Function: _TaskStart($taskname, $folder = "\") ; Description: start a task ; Return Value(s): On Success - Return 1, @ERROR = 0 ; On Failure - Sets @ERROR = 1, Return 0 ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!) ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== Func _TaskStart($taskname, $folder = "\") ;start a task Local Const $VT_EMPTY = 0 Local Const $VT_NULL = 1 If Not _TaskIsValidPlatform() Then Return SetError(2, 2, 0) Local $oService, $oFolder ; $oMyError = ObjEvent("AutoIt.Error", "MyTaskErrFunc") ; Initialize a COM error handler $oService = ObjCreate("Schedule.Service") $oService.Connect() $oFolder = $oService.GetFolder($folder) If IsObj($oFolder) Then $oTask = $oFolder.GetTask($taskname) $instcount = 0 If IsObj($oTask) Then $oTask.Run($VT_NULL) $retval = 1 Else $retval = 0 EndIf Else $retval = 0 EndIf $oService = 0 If $ComErrorFound Then $ComErrorFound = 0 SetError(1) Return $retval Else Return $retval EndIf EndFunc ;==>_TaskStart ;================================================================================== ; Function: _TaskIsRunning($taskname, $folder = "\") ; Description: check if a Task is running ; Return Value(s): On Success - Return 1, @ERROR = 0 ; On Failure - Sets @ERROR = 1, Return 0 ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!) ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== Func _TaskIsRunning($taskname, $folder = "\") If Not _TaskIsValidPlatform() Then Return SetError(2, 2, 0) Local $oService, $oFolder ; $oMyError = ObjEvent("AutoIt.Error", "MyTaskErrFunc") ; Initialize a COM error handler $oService = ObjCreate("Schedule.Service") $oService.Connect() $oFolder = $oService.GetFolder($folder) If IsObj($oFolder) Then $oTask = $oFolder.GetTask($taskname) If IsObj($oTask) Then $instances = $oTask.GetInstances(0) If IsObj($instances) Then If $instances.Count() > 0 Then $retval = 1 Else $retval = 2 EndIf Else $retval = 0 EndIf Else $retval = 0 EndIf Else $retval = 0 EndIf $oService = 0 If $ComErrorFound Then $ComErrorFound = 0 SetError(1) Return $retval Else Return $retval EndIf EndFunc ;==>_TaskIsRunning ;================================================================================== ; Function: _TaskDelete($taskname, $folder = "\") ; Description: delete an existing task ; Return Value(s): On Success - Return 1, @ERROR = 0 ; On Failure - Sets @ERROR = 1, Return 0 ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!) ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== Func _TaskDelete($taskname, $folder = "\") If Not _TaskIsValidPlatform() Then Return SetError(2, 2, 0) Local $oService, $oFolder ; $oMyError = ObjEvent("AutoIt.Error", "MyTaskErrFunc") ; Initialize a COM error handler $oService = ObjCreate("Schedule.Service") $oService.Connect() $oFolder = $oService.GetFolder($folder) If IsObj($oFolder) Then $odeleted = $oFolder.DeleteTask($taskname, 0) $oService = 0 If $ComErrorFound Then $ComErrorFound = 0 SetError(1) Return 0 Else Return 1 EndIf EndFunc ;==>_TaskDelete ;================================================================================== ; Function: _TaskCreate($TaskName, $sDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $LogonType, $RunLevel, $Username, $Password, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True, $active = True,$multiinst=0,$nobatstart=False,$stoponBat=False,$hidden = False, $idle = False, $WakeToRun=False,$timelimit="P1D",$priority = 5, $duration="") ; Description: Creates a scheduled task ; Return Value(s): On Success - Return 1, @ERROR = 0 ; On Failure - Sets @ERROR = 1, Return 0 ; - Wrong OS (Needs Vista or higher) @Error = 2 Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!) ; Example: _TaskCreate("Testname", "Description of this task", 3, "2011-03-30T08:00:00", "", 2, 1, 1, 1, 1, "PT1H", False, 3, 0, "", "", '"c:\windows\system32\notepad.exe"', "", "", True, True,0,False,False,False,False,False,"P1D",5) ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== #cs $TaskName => String, Free text $sDescription => String, Free text $TriggerEvent => http://msdn.microsoft.com/en-us/library/aa383898%28v=VS.85%29.aspx (0: TASK_TRIGGER_EVENT; Triggers the task when a specific event occurs.) => Not working yet 1: TASK_TRIGGER_TIME; Triggers the task at a specific time of day. 2: TASK_TRIGGER_DAILY; Triggers the task on a daily schedule. For example, the task starts at a specific time every day, every-other day, every third day, and so on. 3: TASK_TRIGGER_WEEKLY; Triggers the task on a weekly schedule. For example, the task starts at 8:00 AM on a specific day every week or other week. 4: TASK_TRIGGER_MONTHLY; Triggers the task on a monthly schedule. For example, the task starts on specific days of specific months. 5: TASK_TRIGGER_MONTHLYDOW; Triggers the task on a monthly day-of-week schedule. For example, the task starts on a specific days of the week, weeks of the month, and months of the year. 6: TASK_TRIGGER_IDLE; Triggers the task when the computer goes into an idle state. 7: TASK_TRIGGER_REGISTRATION; Triggers the task when the task is registered. 8: TASK_TRIGGER_BOOT; Triggers the task when the computer boots. => Needs Admin-Rights! 9: TASK_TRIGGER_LOGON; Triggers the task when a specific user logs on. => Needs Admin-Rights! 11:TASK_TRIGGER_SESSION_STATE_CHANGE; Triggers the task when a specific session state changes. => Needs Admin-Rights! $StartTrigger => String with Start time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00" $EndTrigger => String with End time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00" $DaysOfWeek => Integer; 1 = Sunday / 2 = Monday / 4 = Tuesday / 8 = Wednesday / 16 = Thursday / 32 = Friday / 64 = Saturday (http://msdn.microsoft.com/en-us/library/aa384024(v=VS.85).aspx) 3 = Sunday AND Monday! This value works only in TriggerEvent 3 or 5 $DaysOfMonth => Integer http://msdn.microsoft.com/en-us/library/aa380735(VS.85).aspx This value is only noted when $TriggerEvent = 4 Day 1; 1 Day 2; 2 Day 3; 4 Day 4; 8 Day 5; 16 Day 6; 32 Day 7; 64 Day 8; 128 Day 9; 256 Day 10; 512 Day 11; 1024 Day 12; 2048 Day 13; 4096 Day 14; 8192 Day 15; 16384 Day 16; 32768 Day 17; 65536 Day 18; 131072 Day 19; 262144 Day 20; 524288 Day 21; 1048576 Day 22; 2097152 Day 23; 4194304 Day 24; 8388608 Day 25; 16777216 Day 26; 33554432 Day 27; 67108864 Day 28; 134217728 Day 29; 268435456 Day 30; 536870912 Day 31; 1073741824 Last Day; 2147483648 $MonthOfYear => http://msdn.microsoft.com/en-us/library/aa380736(v=VS.85).aspx This value is only noted when $TriggerEvent = 4 January; 1 February; 2 March; 4 April; 8 May; 16 June; 32 July; 64 August; 128 September; 256 October; 512 November; 1024 December; 2048 January + February = 3... $WeeksOfMonth => http://msdn.microsoft.com/en-us/library/aa380733(v=VS.85).aspx This value is only noted when $TriggerEvent = 5 First; 1 Second; 2 Third; 4 Fourth; 8 Fifth; 16 Last; 32 $DaysInterval => Integer of Days Interval; http://msdn.microsoft.com/en-us/library/aa380660(v=VS.85).aspx This value is only noted when $TriggerEvent = 2 $Interval => String for Interval; http://msdn.microsoft.com/en-us/library/aa381138(v=VS.85).aspx PDTHMS (for example, "PT5M" is 5 minutes, "PT1H" is 1 hour, and "PT20M" is 20 minutes). The maximum time allowed is 31 days, and the minimum time allowed is 1 minute. $RepetitionEnabled => True = Interval Trigger enabled / False = Interval Trigger disabled $LogonType => 1= Save password; 2 = TASK_LOGON_S4U / Independant from an userlogin password not saved only local resources; 3 = User must already be logged in ;http://msdn.microsoft.com/en-us/library/aa382075%28v=VS.85%29.aspx $RunLevel => 0 = lowest, 1 = highest; http://msdn.microsoft.com/en-us/library/aa382076%28v=VS.85%29.aspx Highest needs Admin-Rights! $Username => String with domainname "\" and Username. Use blank string ("") for actual user $Password => Password for the specified user $Program => String, Full Path and Programname to run $WorkingDirectory => Optional String $Arguments => Optional String $RunOnlyIfNetworkAvailable => Optional Boolean (Default = True) $active => Optional Boolean Tasks can be created enabled or disabled (True/False) $multiinst => Optional int Default is 0 1: put new task in queu to start after running task has finished 0: run while task is running 2: do not start if running 3: stop running task on start $nobatstart => Optional Boolean Default False do not start when on battery $stoponBat => Optional Boolean Default False stop when going on battery $hidden => Optional Boolean Default False Task is hidden $idle => Optional Boolean Default False run only if idle $WakeToRun => Optional Boolean Default False Wake up the system to run the tasl $timelimit => Optional String Default is "P1D" time after which the task is killed When this parameter is set to Nothing, the execution time limit is infinite. $priority => Optional int Default is 5 Priority with which the task is executed http://msdn.microsoft.com/en-us/library/aa383070(v=VS.85).aspx 0 = Realtime, 10 = Idle $duration => Optional String for duration (should be after $interval, but for compatibillity was added at the end) http://msdn.microsoft.com/en-us/library/aa381132%28v=vs.85%29.aspx $StartWhenAvailable => Optional Boolean Default True starts a missed task as soon as possible #ce Func _TaskCreate($taskname, $sDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $LogonType, $RunLevel, $username, $password, $program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True, $active = True, $multiinst = 0, $nobatstart = False, $stoponBat = False, $hidden = False, $idle = False, $WakeToRun = False, $timelimit = "P1D", $priority = 5, $Duration = "", $StartWhenAvailable = True) If Not _TaskIsValidPlatform() Then Return SetError(2, 2, 0) Local $oService, $oFolder, $oTaskDefinition, $oRegistrationInfo, $oSettings Local $oPrincipal, $oColActions, $oAction, $oTrigger, $oTriggerCol, $oTriggerRepetition If $taskname = "" Or $program = "" Then Return SetError(1, 1, 0) ; $oMyError = ObjEvent("AutoIt.Error", "MyTaskErrFunc") ; Initialize a COM error handler $oService = ObjCreate("Schedule.Service") $oService.Connect() $oFolder = $oService.GetFolder("\") $oTaskDefinition = $oService.NewTask(0) $oRegistrationInfo = $oTaskDefinition.RegistrationInfo() $oRegistrationInfo.Description() = $sDescription $oRegistrationInfo.Author() = @LogonDomain & "\" & @UserName $oSettings = $oTaskDefinition.Settings() $oSettings.MultipleInstances() = $multiinst ;Starts a new instance while an existing instance of the task is running. $oSettings.DisallowStartIfOnBatteries() = $nobatstart $oSettings.StopIfGoingOnBatteries() = $stoponBat $oSettings.AllowHardTerminate() = True $oSettings.StartWhenAvailable() = $StartWhenAvailable ;Start task as soon as possible when task missed $oSettings.RunOnlyIfNetworkAvailable() = $RunOnlyIfNetworkAvailable $oSettings.Enabled() = $active ;True ; The task can be performed only when this setting is True. $oSettings.Hidden() = $hidden $oSettings.RunOnlyIfIdle() = $idle $oSettings.WakeToRun() = $WakeToRun $oSettings.ExecutionTimeLimit() = $timelimit ; When this parameter is set to Nothing, the execution time limit is infinite. $oSettings.Priority() = $priority ;http://msdn.microsoft.com/en-us/library/aa383070(v=VS.85).aspx 0 = Realtime, 10 = Idle $oPrincipal = $oTaskDefinition.Principal() $oPrincipal.Id() = @UserName $oPrincipal.DisplayName() = @UserName $oPrincipal.LogonType() = $LogonType $oPrincipal.RunLevel() = $RunLevel $oTriggerCol = $oTaskDefinition.Triggers() $oTrigger = $oTriggerCol.Create($TriggerEvent) $oTrigger.Id() = "TriggerID" $oTrigger.Enabled() = True $oTrigger.StartBoundary() = $StartTrigger $oTrigger.EndBoundary() = $EndTrigger If $TriggerEvent = 3 Or $TriggerEvent = 5 Then $oTrigger.DaysOfWeek() = $DaysOfWeek EndIf If $TriggerEvent = 4 Then $oTrigger.DaysOfMonth() = $DaysOfMonth $oTrigger.MonthsOfYear() = $MonthOfYear EndIf If $TriggerEvent = 5 Then $oTrigger.WeeksOfMonth() = $WeeksOfMonth EndIf If $TriggerEvent = 2 Then $oTrigger.DaysInterval() = $DaysInterval EndIf If $RepetitionEnabled Then $oTriggerRepetition = $oTrigger.Repetition() $oTriggerRepetition.Interval() = $Interval If $Duration <> "" Then $oTriggerRepetition.Duration() = $Duration EndIf $oColActions = $oTaskDefinition.Actions() $oColActions.Context() = @UserName $oAction = $oTaskDefinition.Actions.Create(0) $oAction.Path() = $program $oAction.WorkingDirectory() = $WorkingDirectory $oAction.Arguments() = $Arguments ; call rootFolder.RegisterTaskDefinition(strTaskName, taskDefinition, FlagTaskCreate, , , LogonTypeInteractive) $oFolder.RegisterTaskDefinition($taskname, $oTaskDefinition, 6, $username, $password, $LogonType, "") $oService = 0 If $ComErrorFound Then $ComErrorFound = 0 SetError(1) Return 0 Else Return 1 EndIf EndFunc ;==>_TaskCreate ;================================================================================== ; Function: _TaskSchedulerAutostart() ; Description: check if the schedulerservice is set to autostart ; Return Value(s): Autostart On - Return 1 ; Autostart Off - Return 0 ; Author(s): allow2010 ; Based on: http://www.autoit.de/index.php?page=Thread&postID=214517#post214517 and on work from Veronesi ; Thread: http://www.autoitscript.com/forum/topic/135994-taskplanner-udf/ ;================================================================================== Func _TaskSchedulerAutostart() If RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Schedule", "Start") > 0 Then Return 1 Else Return 0 EndIf EndFunc ;==>_TaskSchedulerAutostart #ce