TaskScheduler / Aufgabenplaner mittels ObjCreate("Schedule.Service") ansteuern

  • Hiho Forum,

    hat hier vielleicht jemand Erfahrung mit dem scripten des TaskSchedulers / Aufgabenplaners? Ich bekomme meine Tasks einfach nicht registriert. In untem angehängten Code sind massig Links auf die relevanten Seiten bei MSDN eingefügt.

    Gruß

    Spoiler anzeigen
    [autoit]

    #region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_UseUpx=n
    #AutoIt3Wrapper_UseX64=n
    #AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
    #endregion ;**** Directives created by AutoIt3Wrapper_GUI ****

    [/autoit] [autoit][/autoit] [autoit]

    ; schtasks.exe /run /I /TN “Test_KaFu”

    [/autoit] [autoit][/autoit] [autoit]

    ConsoleWrite("IsAdmin() " & IsAdmin() & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    Global Const $NORMAL_PRIORITY_CLASS = 5

    [/autoit] [autoit][/autoit] [autoit]

    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    Global Const $TASK_LOGON_INTERACTIVE_TOKEN = 3 ; User must already be logged on. The task will be run only in an existing interactive session.

    [/autoit] [autoit][/autoit] [autoit]

    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    Global Const $TASK_RUNLEVEL_HIGHEST = 1 ; Tasks will be run with the highest privileges.

    [/autoit] [autoit][/autoit] [autoit]

    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    Global Const $TASK_TRIGGER_EVENT = 0 ; Triggers the task when a specific event occurs.

    [/autoit] [autoit][/autoit] [autoit]

    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    Global Const $TASK_ACTION_EXEC = 0 ; The action performs a command-line operation.

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; http://taskscheduler.codeplex.com/
    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx

    [/autoit] [autoit][/autoit] [autoit]

    $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Initialize a COM error handler

    [/autoit] [autoit][/autoit] [autoit]

    ; Create the TaskService object.
    $oService = ObjCreate("Schedule.Service")
    ConsoleWrite("IsObj($oTaskSchedule) " & IsObj($oService) & @CRLF)
    ConsoleWrite("$oTaskSchedule.service.Connect() " & $oService.Connect() & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    ; Get a folder to create a task definition in.
    $oFolder = $oService.GetFolder("\")
    ConsoleWrite("rootFolder " & IsObj($oFolder) & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    ; The taskDefinition variable is the TaskDefinition object.
    ; The flags parameter is 0 because it is not supported.
    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    $oTaskDefinition = $oService.NewTask(0) ; This parameter is reserved for future use and must be set to 0.
    ConsoleWrite("taskDefinition " & IsObj($oTaskDefinition) & @CRLF)

    [/autoit] [autoit][/autoit] [autoit]

    ; Define information about the task.
    ; Set the registration info for the task by creating the RegistrationInfo object.
    $oRegistrationInfo = $oTaskDefinition.RegistrationInfo()
    ConsoleWrite("oRegistrationInfo " & IsObj($oRegistrationInfo) & @CRLF)
    ;$oRegistrationInfo.Date() = "2011-01-19T08:59:10.4124457"
    $oRegistrationInfo.Author() = @LogonDomain & "\\" & @UserName
    $oRegistrationInfo.Description() = "Beschreibung eines TestTasks"

    [/autoit] [autoit][/autoit] [autoit]

    ; Set the task setting info for the Task Scheduler by creating a TaskSettings object.
    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    $oSettings = $oTaskDefinition.Settings()
    ConsoleWrite("settings " & IsObj($oSettings) & @CRLF)
    ;$oSettings.MultipleInstances() = 0 ; Starts a new instance while an existing instance of the task is running.
    ;$oSettings.DisallowStartIfOnBatteries() = False
    ;$oSettings.StopIfGoingOnBatteries() = False
    ;$oSettings.AllowHardTerminate() = True
    $oSettings.StartWhenAvailable() = False
    ;$oSettings.RunOnlyIfNetworkAvailable() = False
    ;$IdleSettings = $settings.IdleSettings()
    ;$IdleSettings.StopOnIdleEnd() = True
    ;$IdleSettings.RestartOnIdle() = False
    ;$oSettings.AllowDemandStart() = True ; True ; For scripting, gets or sets a Boolean value that indicates that the task can be started by using either the Run command or the Context menu.
    $oSettings.Enabled() = True; True ; The task can be performed only when this setting is True.
    $oSettings.Hidden() = False; False
    ;$oSettings.RunOnlyIfIdle() = False; False
    ; <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    ; <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
    ;$oSettings.WakeToRun() = False; False
    ;$oSettings.ExecutionTimeLimit() = "P3D" ; When this parameter is set to Nothing, the execution time limit is infinite.
    $oSettings.Priority() = $NORMAL_PRIORITY_CLASS

    [/autoit] [autoit][/autoit] [autoit]

    ; Create triggers
    $oColTriggers = $oTaskDefinition.Triggers()
    ConsoleWrite("triggers " & IsObj($oColTriggers) & @CRLF)
    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    ; A maximum of 500 tasks with event subscriptions can be created.

    [/autoit] [autoit][/autoit] [autoit]

    $oTrigger = $oColTriggers.Create($TASK_TRIGGER_EVENT)
    ConsoleWrite("trigger " & IsObj($oTrigger) & @CRLF)
    $oTrigger.Id() = "TestTrigger" ; The identifier for the trigger. This identifier is used by the Task Scheduler for logging purposes.
    $oTrigger.Enabled() = True
    $oTrigger.Subscription() = "";'&lt;QueryList&gt;&lt;Query Id="0" Path="Application"&gt;&lt;Select Path="Application"&gt;*[System[Provider[@Name='Application']]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;'

    [/autoit] [autoit][/autoit] [autoit]

    ; Set the principal for the task
    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    $oPrincipal = $oTaskDefinition.Principal()
    ConsoleWrite("principal " & IsObj($oPrincipal) & @CRLF)
    $oPrincipal.Id() = "TestPrincipal"
    $oPrincipal.DisplayName() = "TestPrincipalDisplayName"
    $oPrincipal.UserId() = @LogonDomain & "\\" & @UserName ; Tasks will be run with the highest privileges.
    $oPrincipal.LogonType() = $TASK_LOGON_INTERACTIVE_TOKEN
    $oPrincipal.RunLevel() = $TASK_RUNLEVEL_HIGHEST
    ; When specifying an account, remember to properly use the double backslash in code to specify the domain and user name. For example, use DOMAIN\\UserName to specify a value for the UserId property.

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; Create the action for the task to execute.
    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    $oColActions = $oTaskDefinition.Actions()
    $oColActions.Context() = "TestPrincipal" ; The identifier of the principal for the task. The identifier that is specified here must match the identifier that is specified in the Id property of the IPrincipal interface that defines the principal.
    $oAction = $oColActions.Create($TASK_ACTION_EXEC) ; TASK_ACTION_EXEC
    ConsoleWrite("Action " & IsObj($oAction) & @CRLF)
    ;$Action.Type() = 0 ; already in create(0)
    ;$oAction.ID() = "Test"
    $oAction.Path() = "C:\Windows\System32\notepad.exe"
    $oAction.WorkingDirectory() = "C:\"
    $oAction.Arguments() = ""

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; Register (create) the task.
    ; http://msdn.microsoft.com/en-us/library/…28VS.85%29.aspx
    #cs
    Function RegisterTaskDefinition( _
    ByVal path, _
    ByVal definition, _
    ByVal flags, _
    ByVal userId, _
    ByVal password, _
    ByVal longonType, _
    [ ByVal sddl ], _
    ByRef task _
    )
    #ce
    ; TASK_CREATE_OR_UPDATE = 0x6

    [/autoit] [autoit][/autoit] [autoit]

    Global Const $TASK_CREATE_OR_UPDATE = 6
    $sUsername = "";@LogonDomain & "\\" & @UserName
    $sPassword = ""
    $iSecurityDescriptor = ""

    [/autoit] [autoit][/autoit] [autoit]

    ;Global $oRegisteredTask; = $oTaskDefinition.RegisteredTask()
    ; call rootFolder.RegisterTaskDefinition(strTaskName, taskDefinition, FlagTaskCreate, , , LogonTypeInteractive)
    Global $oRegisteredTask
    $oFolder.RegisterTaskDefinition("Test_KaFu", $oTaskDefinition, $TASK_CREATE_OR_UPDATE, "", "", $TASK_LOGON_INTERACTIVE_TOKEN, $oRegisteredTask);, $iSecurityDescriptor);, $oRegisteredTask)
    MsgBox(0, "", IsObj($oRegisteredTask))

    [/autoit] [autoit][/autoit] [autoit]

    ; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    ; ExecAction Object
    ; Scripting object that represents an action that executes a command-line operation.

    [/autoit] [autoit][/autoit] [autoit]

    ; Security Contexts for Tasks
    ; http://msdn.microsoft.com/en-us/library/…v=vs.85%29.aspx

    [/autoit] [autoit][/autoit] [autoit]

    ; http://msdn.microsoft.com/en-us/library/…v=vs.85%29.aspx
    ;Principal.RunLevel(1) ; Tasks will be run with the highest privileges.

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    $sCommand "schtasks.exe /RU " & @UserName & " /SC ONEVENT /TN TestShortcut /TR ""\"""& $sTargetFile & """\"" " & """\""" & $sCommandline & """\"" /IT

    [/autoit] [autoit][/autoit] [autoit]

    /EC ChannelName

    [/autoit] [autoit][/autoit] [autoit]

    schtasks /create /tn "My App Job1" /tr "\"C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE\" C:\text.doc"
    /sc DAILY /st 05:00:00 /ru <username> /rp
    <password>

    [/autoit] [autoit][/autoit] [autoit]

    schtasks /run /tn “TASKNAMEINQUOTES”

    [/autoit] [autoit][/autoit] [autoit]

    It seems that "Run with highest privileges" option will not take precedence of the UAC. While the Admin Approval Mode for built-in Administrator account is enabled, UAC will still ask for approval
    according to the settings on the Behavior of elevation to prompt for the administrators.
    Check whether the "User Account Control: Admin Approval Mode for built-in Administrator account" is enabled. If yes, disable it or change the setting on "User Account Control:
    Behavior of elevation to prompt for the administrators" to elevate without prompting.
    - Did you try to right click the stsadm.exe and in the security tab add the backup operator account in the security with full control and try.
    #ce

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func MyErrFunc()
    MsgBox(0, "AutoItCOM Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _
    "err.description is: " & @TAB & $oMyError.description & @CRLF & _
    "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
    "err.number is: " & @TAB & Hex($oMyError.number, 8) & @CRLF & _
    "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
    "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
    "err.source is: " & @TAB & $oMyError.source & @CRLF & _
    "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
    "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
    )

    [/autoit] [autoit][/autoit] [autoit]

    Local $err = $oMyError.number
    If $err = 0 Then $err = -1

    [/autoit] [autoit][/autoit] [autoit]

    $g_eventerror = $err ; to check for after this function returns
    EndFunc ;==>MyErrFunc

    [/autoit]

    Einmal editiert, zuletzt von KaFu (23. Januar 2011 um 13:49)

  • Danke für die Antwort Shadow :).

    Habs hinbekommen, die oben angehängte Source funktioniert grundsätzlich, ich hatte nur ein paar Parameter so gegensätzlich gesetzt, dass der Taskscheduler den Task nicht akzeptiert hatte. Anfrage ist somit gelöst... stay tuned for a full-blown app on this :)...

  • Hallo KaFu,

    Du hast das anscheinend erfolgreich lösen können.
    Hast Du daraus mal eine Funktion gemacht, welcher man sämtliche Parameter übergeben kann?
    Ich meine so, dass ich mit einem Funktionsaufruf einen Task erstellen kann und bei dem sämtliche Möglichkeiten der Taskerstellung ausgeschöpft werden können?

    Wäre toll, wenn Du so eine Funktion (falls vorhanden oder in Kürze machbar) hier noch einstellen könntest.

    Vielen Dank!
    Veronesi

  • Hallo Diggidie,

    vielen Dank für die Antwort. Leider nützt mir das nichts, da ich es momentan genau über die schtasks erzeuge.
    Mein Problem ist, dass ich Zugriff auf u.a. folgende Dinge haben müsste:
    - Aufgabe nur starten, falls Computer im Netzbetrieb ausgeführt wird
    - Nur starten, wenn folgende Netzwerkverbindung verfügbar ist: .....
    - Aufgabe so schnell wie möglich nach einem verpassten Start ausführen
    - Falls Aufgabe scheitert, neu starten alle:...

    Das kann ich leider nicht als Parameter bei der schtasks mitgeben.
    Hat dazu jemand eine Idee?

    PS: Mein Problem ist, dass ich das Skript aus dem ersten Beitrag nicht zum laufen kriege (Win7 x64)

  • So, nun konnte ich das Beispiel zum laufen bringen.

    Dann habe ich noch (wirklich ganz kurz) eine kleine Funktion daraus gemacht, mit welcher man ziemlich viele Einstellmöglichkeiten hat, um den Task zu erstellen.
    Damit das Ganze auch korrekt dokumentiert ist, stelle ich es hier nun ein!

    Spoiler anzeigen
    [autoit]

    #include-once
    Dim $oMyError, $ErrorFound = False

    [/autoit] [autoit][/autoit] [autoit]

    ;==================================================================================
    ; Function: _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    ; Description: Creates a scheduled task
    ; Parameter(s): $h_Window - Handle to the Window to update
    ; $i_Mode - Update Mode; 0=Small Update; 1=Normal Update.
    ; Return Value(s): On Success - Return 1, @ERROR = 0
    ; On Failure - Sets @ERROR = 1, Return 0
    ; Author(s): Unknown (http://www.autoit.de/index.php?page…4517#post214517)
    ; Changes: Veronesi
    ; Note(s):
    ; Example: _CreateTask("Testname", "Description of this task", 3, "2011-03-30T08:00:00", "2011-03-30T08:00:00", 2, 1, 1, 1, "c:\windows\system32\notepad.exe", "c:\windows\system32", "", True)
    ;==================================================================================

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    $TaskName => String, Free text

    [/autoit] [autoit][/autoit] [autoit]

    $TaskDescription => String, Free text

    [/autoit] [autoit][/autoit] [autoit]

    $TriggerEvent => http://msdn.microsoft.com/en-us/library/…v=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!

    [/autoit] [autoit][/autoit] [autoit]

    $StartTrigger => String with Start time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00"

    [/autoit] [autoit][/autoit] [autoit]

    $EndTrigger => String with End time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00"

    [/autoit] [autoit][/autoit] [autoit]

    $DaysOfWeek => Integer; 1 = Sunday / 2 = Monday / 4 = Tuesday / 8 = Wednesday / 16 = Thursday / 32 = Friday / 64 = Saturday (http://msdn.microsoft.com/en-us/library/…4(v=VS.85).aspx)
    3 = Sunday AND Monday!
    This value works only in TriggerEvent 3 or 5

    [/autoit] [autoit][/autoit] [autoit]

    $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

    [/autoit] [autoit][/autoit] [autoit]

    $MonthOfYear => http://msdn.microsoft.com/en-us/library/…6(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...

    [/autoit] [autoit][/autoit] [autoit]

    $WeeksOfMonth => http://msdn.microsoft.com/en-us/library/…3(v=VS.85).aspx
    This value is only noted when $TriggerEvent = 5
    First; 1
    Second; 2
    Third; 4
    Fourth; 8
    Fifth; 16
    Last; 32

    [/autoit] [autoit][/autoit] [autoit]

    $Program => String, Full Path and Programname to run

    [/autoit] [autoit][/autoit] [autoit]

    $WorkingDirectory => Optional String

    [/autoit] [autoit][/autoit] [autoit]

    $Arguments => Optional String

    [/autoit] [autoit][/autoit] [autoit]

    $RunOnlyIfNetworkAvailable => Optional Boolean (Default = True)

    [/autoit] [autoit][/autoit] [autoit]

    #ce

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    Local $oService, $oFolder, $oTaskDefinition, $oRegistrationInfo, $oSettings
    Local $oPrincipal, $oColActions, $oAction, $oTrigger, $oColTriggers

    [/autoit] [autoit][/autoit] [autoit]

    If $TaskName = "" Or $Program = "" Then Return SetError (1,1,1)

    [/autoit] [autoit][/autoit] [autoit]

    ;http://www.scriptinganswers.com/forum2/forum_posts.asp?TID=3282
    ;http://www.autoit.de/index.php?page…4517#post214517
    ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    $oMyError = ObjEvent("AutoIt.Error", "MyCreateTaskErrFunc") ; Initialize a COM error handler
    $oService = ObjCreate("Schedule.Service")
    $oService.Connect()

    [/autoit] [autoit][/autoit] [autoit]

    $oFolder = $oService.GetFolder("\")
    $oTaskDefinition = $oService.NewTask(0)

    [/autoit] [autoit][/autoit] [autoit]

    $oRegistrationInfo = $oTaskDefinition.RegistrationInfo()
    $oRegistrationInfo.Description() = $TaskDescription
    $oRegistrationInfo.Author() = @LogonDomain & "\" & @UserName

    [/autoit] [autoit][/autoit] [autoit]

    $oSettings = $oTaskDefinition.Settings()
    $oSettings.MultipleInstances() = 0 ;Starts a new instance while an existing instance of the task is running.
    $oSettings.DisallowStartIfOnBatteries() = False
    $oSettings.StopIfGoingOnBatteries() = False
    $oSettings.AllowHardTerminate() = True
    $oSettings.StartWhenAvailable() = True ;Start task as soon as possible when task missed
    $oSettings.RunOnlyIfNetworkAvailable() = $RunOnlyIfNetworkAvailable

    [/autoit] [autoit][/autoit] [autoit]

    $oSettings.Enabled() = True ;True ; The task can be performed only when this setting is True.
    $oSettings.Hidden() = False
    $oSettings.RunOnlyIfIdle() = False
    $oSettings.WakeToRun() = False
    $oSettings.ExecutionTimeLimit() = "P1D" ; When this parameter is set to Nothing, the execution time limit is infinite.
    $oSettings.Priority() = 5 ;http://msdn.microsoft.com/en-us/library/…0(v=VS.85).aspx 0 = Realtime, 10 = Idle

    [/autoit] [autoit][/autoit] [autoit]

    $oPrincipal = $oTaskDefinition.Principal()
    $oPrincipal.Id() = @UserName
    $oPrincipal.DisplayName() = @UserName
    $oPrincipal.LogonType() = 3 ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx 3 = User must already be logged in
    $oPrincipal.RunLevel() = 0 ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx 0 = lowest, 1 = highest

    [/autoit] [autoit][/autoit] [autoit]

    $oColTriggers = $oTaskDefinition.Triggers()
    $oTrigger = $oColTriggers.Create($TriggerEvent)

    [/autoit] [autoit][/autoit] [autoit]

    $oTrigger.Id() = "TriggerID"
    $oTrigger.Enabled() = True
    $oTrigger.StartBoundary() = $StartTrigger
    $oTrigger.EndBoundary() = $EndTrigger
    If $TriggerEvent = 3 Or $TriggerEvent = 5 Then
    $oTrigger.DaysOfWeek() = $DaysOfWeek
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $TriggerEvent = 4 Then
    $oTrigger.DaysOfMonth() = $DaysOfMonth
    $oTrigger.MonthsOfYear() = $MonthOfYear
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $TriggerEvent = 5 Then
    $oTrigger.WeeksOfMonth() = $WeeksOfMonth
    EndIf

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $oColActions = $oTaskDefinition.Actions()
    $oColActions.Context() = @UserName
    $oAction = $oTaskDefinition.Actions.Create(0)
    $oAction.Path() = $Program
    $oAction.WorkingDirectory() = $WorkingDirectory
    $oAction.Arguments() = $Arguments

    [/autoit] [autoit][/autoit] [autoit]

    ; call rootFolder.RegisterTaskDefinition(strTaskName, taskDefinition, FlagTaskCreate, , , LogonTypeInteractive)
    $oFolder.RegisterTaskDefinition($TaskName, $oTaskDefinition, 6, "", "", 3, "")

    [/autoit] [autoit][/autoit] [autoit]

    If $ErrorFound Then Return SetError(1,1,0)
    Return 1
    EndFunc ;==>_CreateTask

    [/autoit] [autoit][/autoit] [autoit]

    Func MyCreateTaskErrFunc()
    $ErrorFound = True
    Return 0

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    MsgBox(0, "AutoItCOM Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _
    "err.description is: " & @TAB & $oMyError.description & @CRLF & _
    "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
    "err.number is: " & @TAB & Hex($oMyError.number, 8) & @CRLF & _
    "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
    "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
    "err.source is: " & @TAB & $oMyError.source & @CRLF & _
    "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
    "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
    )

    [/autoit] [autoit][/autoit] [autoit]

    Local $err = $oMyError.number
    Local $g_eventerror
    If $err = 0 Then $err = -1

    [/autoit] [autoit][/autoit] [autoit]

    $g_eventerror = $err ; to check for after this function returns
    EndFunc ;==>MyCreateTaskErrFunc

    [/autoit]

    Wenn jemand das noch erweitern möchte, damit man noch mehr Optionen hat, so kann er es gerne wieder hier einstellen, so dass dies allen nützt!
    Vor allem folgenden Optionen wäre ich interessiert:
    - Nur Ausführen, wenn der Benutzer angemeldet ist / bw. Unabhängig davon
    - Mit höchsten Privilegien
    - Beim Ausführen der Aufgaben folgendes Benutzerkonto verwenden.....

    Danke + Grüsse
    Veronesi

    Einmal editiert, zuletzt von veronesi (30. März 2011 um 08:25)

  • Sieht gut aus. DANKE.
    Werde ich auf alle Fälle in meinem Progrämmchen verwenden. Erspart mir einiges an Kopfweh, vorallem da ich mich erst seit kurzen mit Objekten befasse.

  • So, nun kann man auch auf täglicher Basis das Tagesinterval und die Wiederholung (stündlich, minütlich, alle 25 Minuten) eingeben.
    Hier das neue Program:

    Spoiler anzeigen
    [autoit]

    #include-once
    Dim $oMyError, $ErrorFound = False

    [/autoit] [autoit][/autoit] [autoit]

    ;==================================================================================
    ; Function: _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    ; Description: Creates a scheduled task
    ; Parameter(s): $h_Window - Handle to the Window to update
    ; $i_Mode - Update Mode; 0=Small Update; 1=Normal Update.
    ; Return Value(s): On Success - Return 1, @ERROR = 0
    ; On Failure - Sets @ERROR = 1, Return 0
    ; Author(s): Unknown (http://www.autoit.de/index.php?page…4517#post214517)
    ; Changes: Veronesi
    ; Note(s):
    ; Example: _CreateTask("Testname", "Description of this task", 3, "2011-03-30T08:00:00", "2011-03-30T08:00:00", 2, 1, 1, 1, 1, "PT1H", False, "c:\windows\system32\notepad.exe", "c:\windows\system32", "", True)
    ;==================================================================================

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    $TaskName => String, Free text

    [/autoit] [autoit][/autoit] [autoit]

    $TaskDescription => String, Free text

    [/autoit] [autoit][/autoit] [autoit]

    $TriggerEvent => http://msdn.microsoft.com/en-us/library/…v=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!

    [/autoit] [autoit][/autoit] [autoit]

    $StartTrigger => String with Start time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00"

    [/autoit] [autoit][/autoit] [autoit]

    $EndTrigger => String with End time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00"

    [/autoit] [autoit][/autoit] [autoit]

    $DaysOfWeek => Integer; 1 = Sunday / 2 = Monday / 4 = Tuesday / 8 = Wednesday / 16 = Thursday / 32 = Friday / 64 = Saturday (http://msdn.microsoft.com/en-us/library/…4(v=VS.85).aspx)
    3 = Sunday AND Monday!
    This value works only in TriggerEvent 3 or 5

    [/autoit] [autoit][/autoit] [autoit]

    $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

    [/autoit] [autoit][/autoit] [autoit]

    $MonthOfYear => http://msdn.microsoft.com/en-us/library/…6(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...

    [/autoit] [autoit][/autoit] [autoit]

    $WeeksOfMonth => http://msdn.microsoft.com/en-us/library/…3(v=VS.85).aspx
    This value is only noted when $TriggerEvent = 5
    First; 1
    Second; 2
    Third; 4
    Fourth; 8
    Fifth; 16
    Last; 32

    [/autoit] [autoit][/autoit] [autoit]

    $DaysInterval => Integer of Days Interval; http://msdn.microsoft.com/en-us/library/…0(v=VS.85).aspx
    This value is only noted when $TriggerEvent = 2

    [/autoit] [autoit][/autoit] [autoit]

    $Interval => String for Interval; http://msdn.microsoft.com/en-us/library/…8(v=VS.85).aspx
    P<days>DT<hours>H<minutes>M<seconds>S (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.

    [/autoit] [autoit][/autoit] [autoit]

    $RepetitionEnabled => True = Interval Trigger enabled / False = Interval Trigger disabled

    [/autoit] [autoit][/autoit] [autoit]

    $Program => String, Full Path and Programname to run

    [/autoit] [autoit][/autoit] [autoit]

    $WorkingDirectory => Optional String

    [/autoit] [autoit][/autoit] [autoit]

    $Arguments => Optional String

    [/autoit] [autoit][/autoit] [autoit]

    $RunOnlyIfNetworkAvailable => Optional Boolean (Default = True)

    [/autoit] [autoit][/autoit] [autoit]

    #ce

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    Local $oService, $oFolder, $oTaskDefinition, $oRegistrationInfo, $oSettings
    Local $oPrincipal, $oColActions, $oAction, $oTrigger, $oColTriggers, $oTriggerRepetition

    [/autoit] [autoit][/autoit] [autoit]

    If $TaskName = "" Or $Program = "" Then Return SetError (1,1,1)

    [/autoit] [autoit][/autoit] [autoit]

    ;http://www.scriptinganswers.com/forum2/forum_posts.asp?TID=3282
    ;http://www.autoit.de/index.php?page…4517#post214517
    ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    $oMyError = ObjEvent("AutoIt.Error", "MyCreateTaskErrFunc") ; Initialize a COM error handler
    $oService = ObjCreate("Schedule.Service")
    $oService.Connect()

    [/autoit] [autoit][/autoit] [autoit]

    $oFolder = $oService.GetFolder("\")
    $oTaskDefinition = $oService.NewTask(0)

    [/autoit] [autoit][/autoit] [autoit]

    $oRegistrationInfo = $oTaskDefinition.RegistrationInfo()
    $oRegistrationInfo.Description() = $TaskDescription
    $oRegistrationInfo.Author() = @LogonDomain & "\" & @UserName

    [/autoit] [autoit][/autoit] [autoit]

    $oSettings = $oTaskDefinition.Settings()
    $oSettings.MultipleInstances() = 0 ;Starts a new instance while an existing instance of the task is running.
    $oSettings.DisallowStartIfOnBatteries() = False
    $oSettings.StopIfGoingOnBatteries() = False
    $oSettings.AllowHardTerminate() = True
    $oSettings.StartWhenAvailable() = True ;Start task as soon as possible when task missed
    $oSettings.RunOnlyIfNetworkAvailable() = $RunOnlyIfNetworkAvailable

    [/autoit] [autoit][/autoit] [autoit]

    $oSettings.Enabled() = True ;True ; The task can be performed only when this setting is True.
    $oSettings.Hidden() = False
    $oSettings.RunOnlyIfIdle() = False
    $oSettings.WakeToRun() = False
    $oSettings.ExecutionTimeLimit() = "P1D" ; When this parameter is set to Nothing, the execution time limit is infinite.
    $oSettings.Priority() = 5 ;http://msdn.microsoft.com/en-us/library/…0(v=VS.85).aspx 0 = Realtime, 10 = Idle

    [/autoit] [autoit][/autoit] [autoit]

    $oPrincipal = $oTaskDefinition.Principal()
    $oPrincipal.Id() = @UserName
    $oPrincipal.DisplayName() = @UserName
    $oPrincipal.LogonType() = 3 ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx 3 = User must already be logged in
    $oPrincipal.RunLevel() = 0 ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx 0 = lowest, 1 = highest

    [/autoit] [autoit][/autoit] [autoit]

    $oColTriggers = $oTaskDefinition.Triggers()
    $oTrigger = $oColTriggers.Create($TriggerEvent)

    [/autoit] [autoit][/autoit] [autoit]

    $oTrigger.Id() = "TriggerID"
    $oTrigger.Enabled() = True
    $oTrigger.StartBoundary() = $StartTrigger
    $oTrigger.EndBoundary() = $EndTrigger
    If $TriggerEvent = 3 Or $TriggerEvent = 5 Then
    $oTrigger.DaysOfWeek() = $DaysOfWeek
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $TriggerEvent = 4 Then
    $oTrigger.DaysOfMonth() = $DaysOfMonth
    $oTrigger.MonthsOfYear() = $MonthOfYear
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $TriggerEvent = 5 Then
    $oTrigger.WeeksOfMonth() = $WeeksOfMonth
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $TriggerEvent = 2 Then
    $oTrigger.DaysInterval() = $DaysInterval
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $RepetitionEnabled Then
    $oTriggerRepetition = $oTrigger.Repetition()
    $oTriggerRepetition.Interval() = $Interval
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $oColActions = $oTaskDefinition.Actions()
    $oColActions.Context() = @UserName
    $oAction = $oTaskDefinition.Actions.Create(0)
    $oAction.Path() = $Program
    $oAction.WorkingDirectory() = $WorkingDirectory
    $oAction.Arguments() = $Arguments

    [/autoit] [autoit][/autoit] [autoit]

    ; call rootFolder.RegisterTaskDefinition(strTaskName, taskDefinition, FlagTaskCreate, , , LogonTypeInteractive)
    $oFolder.RegisterTaskDefinition($TaskName, $oTaskDefinition, 6, "", "", 3, "")

    [/autoit] [autoit][/autoit] [autoit]

    If $ErrorFound Then Return SetError(1,1,0)
    Return 1
    EndFunc ;==>_CreateTask

    [/autoit] [autoit][/autoit] [autoit]

    Func MyCreateTaskErrFunc()
    $ErrorFound = True
    Return 0

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    MsgBox(0, "AutoItCOM Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _
    "err.description is: " & @TAB & $oMyError.description & @CRLF & _
    "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
    "err.number is: " & @TAB & Hex($oMyError.number, 8) & @CRLF & _
    "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
    "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
    "err.source is: " & @TAB & $oMyError.source & @CRLF & _
    "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
    "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
    )

    [/autoit] [autoit][/autoit] [autoit]

    Local $err = $oMyError.number
    Local $g_eventerror
    If $err = 0 Then $err = -1

    [/autoit] [autoit][/autoit] [autoit]

    $g_eventerror = $err ; to check for after this function returns
    EndFunc ;==>MyCreateTaskErrFunc

    [/autoit]

    Einmal editiert, zuletzt von veronesi (30. März 2011 um 10:03)

  • Hallo zusammen,

    so, nun konnte ich auch noch die letzten Punkte bewältigen

    - Nur Ausführen, wenn der Benutzer angemeldet ist / bw. Unabhängig davon => Funktioniert
    - Mit höchsten Privilegien => Funktioniert
    - Beim Ausführen der Aufgaben folgendes Benutzerkonto verwenden..... =>Funktioniert

    Hier deshalb die (momentan) letzte und aktuellste Version des Programmes.
    Es sind zwar recht viele mögliche Parameter, doch ich denke ich habe sie recht gut beschrieben!


    Spoiler anzeigen
    [autoit]

    #include-once
    Dim $oMyError, $ErrorFound = False

    [/autoit] [autoit][/autoit] [autoit]

    ;==================================================================================
    ; Function: _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $LogonType, $RunLevel, $Username, $Password, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    ; Description: Creates a scheduled task
    ; Parameter(s): $h_Window - Handle to the Window to update
    ; $i_Mode - Update Mode; 0=Small Update; 1=Normal Update.
    ; 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): Unknown (http://www.autoit.de/index.php?page…4517#post214517)
    ; Changes: Veronesi
    ; Note(s): Works only on Win7 and above (Perhaps also Win Vista, but not tested!)
    ; Example: _CreateTask("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)
    ;==================================================================================

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    $TaskName => String, Free text

    [/autoit] [autoit][/autoit] [autoit]

    $TaskDescription => String, Free text

    [/autoit] [autoit][/autoit] [autoit]

    $TriggerEvent => http://msdn.microsoft.com/en-us/library/…v=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!

    [/autoit] [autoit][/autoit] [autoit]

    $StartTrigger => String with Start time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00"

    [/autoit] [autoit][/autoit] [autoit]

    $EndTrigger => String with End time / date (Year-Month-DayTHour:Min:Sec) E.g. "2011-03-30T08:00:00"

    [/autoit] [autoit][/autoit] [autoit]

    $DaysOfWeek => Integer; 1 = Sunday / 2 = Monday / 4 = Tuesday / 8 = Wednesday / 16 = Thursday / 32 = Friday / 64 = Saturday (http://msdn.microsoft.com/en-us/library/…4(v=VS.85).aspx)
    3 = Sunday AND Monday!
    This value works only in TriggerEvent 3 or 5

    [/autoit] [autoit][/autoit] [autoit]

    $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

    [/autoit] [autoit][/autoit] [autoit]

    $MonthOfYear => http://msdn.microsoft.com/en-us/library/…6(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...

    [/autoit] [autoit][/autoit] [autoit]

    $WeeksOfMonth => http://msdn.microsoft.com/en-us/library/…3(v=VS.85).aspx
    This value is only noted when $TriggerEvent = 5
    First; 1
    Second; 2
    Third; 4
    Fourth; 8
    Fifth; 16
    Last; 32

    [/autoit] [autoit][/autoit] [autoit]

    $DaysInterval => Integer of Days Interval; http://msdn.microsoft.com/en-us/library/…0(v=VS.85).aspx
    This value is only noted when $TriggerEvent = 2

    [/autoit] [autoit][/autoit] [autoit]

    $Interval => String for Interval; http://msdn.microsoft.com/en-us/library/…8(v=VS.85).aspx
    P<days>DT<hours>H<minutes>M<seconds>S (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.

    [/autoit] [autoit][/autoit] [autoit]

    $RepetitionEnabled => True = Interval Trigger enabled / False = Interval Trigger disabled

    [/autoit] [autoit][/autoit] [autoit]

    $LogonType => 2 = TASK_LOGON_S4U / Independant from an userlogin
    3 = User must already be logged in
    ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx

    [/autoit] [autoit][/autoit] [autoit]

    $RunLevel => 0 = lowest, 1 = highest; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    Highest Need Admin-Rights!

    [/autoit] [autoit][/autoit] [autoit]

    $Username => String with domainname "\" and Username. Use blank string ("") for actual user

    [/autoit] [autoit][/autoit] [autoit]

    $Password => Password for the specified user

    [/autoit] [autoit][/autoit] [autoit]

    $Program => String, Full Path and Programname to run

    [/autoit] [autoit][/autoit] [autoit]

    $WorkingDirectory => Optional String

    [/autoit] [autoit][/autoit] [autoit]

    $Arguments => Optional String

    [/autoit] [autoit][/autoit] [autoit]

    $RunOnlyIfNetworkAvailable => Optional Boolean (Default = True)

    [/autoit] [autoit][/autoit] [autoit]

    #ce

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _CreateTask($TaskName, $TaskDescription, $TriggerEvent, $StartTrigger, $EndTrigger, $DaysOfWeek, $DaysOfMonth, $MonthOfYear, $WeeksOfMonth, $DaysInterval, $Interval, $RepetitionEnabled, $LogonType, $RunLevel, $Username, $Password, $Program, $WorkingDirectory = "", $Arguments = "", $RunOnlyIfNetworkAvailable = True)
    If @OSVersion = "WIN_XP" Or @OSVersion = "WIN_XPe" Or @OSVersion = "WIN_2000" Then Return SetError (2, 2, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $oService, $oFolder, $oTaskDefinition, $oRegistrationInfo, $oSettings
    Local $oPrincipal, $oColActions, $oAction, $oTrigger, $oColTriggers, $oTriggerRepetition

    [/autoit] [autoit][/autoit] [autoit]

    If $TaskName = "" Or $Program = "" Then Return SetError (1,1,1)

    [/autoit] [autoit][/autoit] [autoit]

    ;http://www.scriptinganswers.com/forum2/forum_posts.asp?TID=3282
    ;http://www.autoit.de/index.php?page…4517#post214517
    ;http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
    $oMyError = ObjEvent("AutoIt.Error", "MyCreateTaskErrFunc") ; Initialize a COM error handler
    $oService = ObjCreate("Schedule.Service")
    $oService.Connect()

    [/autoit] [autoit][/autoit] [autoit]

    $oFolder = $oService.GetFolder("\")
    $oTaskDefinition = $oService.NewTask(0)

    [/autoit] [autoit][/autoit] [autoit]

    $oRegistrationInfo = $oTaskDefinition.RegistrationInfo()
    $oRegistrationInfo.Description() = $TaskDescription
    $oRegistrationInfo.Author() = @LogonDomain & "\" & @UserName

    [/autoit] [autoit][/autoit] [autoit]

    $oSettings = $oTaskDefinition.Settings()
    $oSettings.MultipleInstances() = 0 ;Starts a new instance while an existing instance of the task is running.
    $oSettings.DisallowStartIfOnBatteries() = False
    $oSettings.StopIfGoingOnBatteries() = False
    $oSettings.AllowHardTerminate() = True
    $oSettings.StartWhenAvailable() = True ;Start task as soon as possible when task missed
    $oSettings.RunOnlyIfNetworkAvailable() = $RunOnlyIfNetworkAvailable

    [/autoit] [autoit][/autoit] [autoit]

    $oSettings.Enabled() = True ;True ; The task can be performed only when this setting is True.
    $oSettings.Hidden() = False
    $oSettings.RunOnlyIfIdle() = False
    $oSettings.WakeToRun() = False
    $oSettings.ExecutionTimeLimit() = "P1D" ; When this parameter is set to Nothing, the execution time limit is infinite.
    $oSettings.Priority() = 5 ;http://msdn.microsoft.com/en-us/library/…0(v=VS.85).aspx 0 = Realtime, 10 = Idle

    [/autoit] [autoit][/autoit] [autoit]

    $oPrincipal = $oTaskDefinition.Principal()
    $oPrincipal.Id() = @UserName
    $oPrincipal.DisplayName() = @UserName
    $oPrincipal.LogonType() = $LogonType
    $oPrincipal.RunLevel() = $RunLevel

    [/autoit] [autoit][/autoit] [autoit]

    $oColTriggers = $oTaskDefinition.Triggers()
    $oTrigger = $oColTriggers.Create($TriggerEvent)

    [/autoit] [autoit][/autoit] [autoit]

    $oTrigger.Id() = "TriggerID"
    $oTrigger.Enabled() = True
    $oTrigger.StartBoundary() = $StartTrigger
    $oTrigger.EndBoundary() = $EndTrigger
    If $TriggerEvent = 3 Or $TriggerEvent = 5 Then
    $oTrigger.DaysOfWeek() = $DaysOfWeek
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $TriggerEvent = 4 Then
    $oTrigger.DaysOfMonth() = $DaysOfMonth
    $oTrigger.MonthsOfYear() = $MonthOfYear
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $TriggerEvent = 5 Then
    $oTrigger.WeeksOfMonth() = $WeeksOfMonth
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $TriggerEvent = 2 Then
    $oTrigger.DaysInterval() = $DaysInterval
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    If $RepetitionEnabled Then
    $oTriggerRepetition = $oTrigger.Repetition()
    $oTriggerRepetition.Interval() = $Interval
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $oColActions = $oTaskDefinition.Actions()
    $oColActions.Context() = @UserName
    $oAction = $oTaskDefinition.Actions.Create(0)
    $oAction.Path() = $Program
    $oAction.WorkingDirectory() = $WorkingDirectory
    $oAction.Arguments() = $Arguments

    [/autoit] [autoit][/autoit] [autoit]

    ; call rootFolder.RegisterTaskDefinition(strTaskName, taskDefinition, FlagTaskCreate, , , LogonTypeInteractive)
    $oFolder.RegisterTaskDefinition($TaskName, $oTaskDefinition, 6, $Username, $Password, $LogonType, "")

    [/autoit] [autoit][/autoit] [autoit]

    If $ErrorFound Then Return SetError(1,1,0)
    Return 1
    EndFunc ;==>_CreateTask

    [/autoit] [autoit][/autoit] [autoit]

    Func MyCreateTaskErrFunc()
    $ErrorFound = True
    Return 0

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    MsgBox(0, "AutoItCOM Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _
    "err.description is: " & @TAB & $oMyError.description & @CRLF & _
    "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
    "err.number is: " & @TAB & Hex($oMyError.number, 8) & @CRLF & _
    "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
    "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
    "err.source is: " & @TAB & $oMyError.source & @CRLF & _
    "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
    "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
    )

    [/autoit] [autoit][/autoit] [autoit]

    Local $err = $oMyError.number
    Local $g_eventerror
    If $err = 0 Then $err = -1

    [/autoit] [autoit][/autoit] [autoit]

    $g_eventerror = $err ; to check for after this function returns
    EndFunc ;==>MyCreateTaskErrFunc

    [/autoit]

    Grüsse Veronesi

    Edit: 08.04.2011 / 15:44 Kleine Ergänzung im Script: Nur ab Win7 und höher. Möglicherweise geht Vista auch.

    2 Mal editiert, zuletzt von veronesi (8. April 2011 um 15:45)

  • Vor einiger Zeit habe ich einmal ähnliches versucht. Ich weiß aber nicht mehr warum ich es nicht weiter verfolgt hatte.

    Jedenfalls wollte ich das in der Include-Datei vorhandene Beispiel testen. Aber es kommt dort schon zu folgender Fehlermeldung, die ich beim besten Willen ncht weg bekommen:
    autoit.de/wcf/attachment/13044/

    System ist: Windows XP SP3 mit Adminrechten.

    Bilder

    Zur Nutzung dieses Forum's, ist ein Übersetzer für folgende Begriffe unerlässlich:

    "On-Bort, weier, verscheiden, schädliges, Butten steyling, näckstet, Parr, Porblem, scripe, Kompletenz, harken, manuel zu extramieren, geckukt, würglich, excell, acces oder Compilevorgeng"

  • Na hab ich doch ! Und als Beispiel, wie beschrieben, den Code aus Zeile 14 der UDF.

    Hab es gerade mal unter Win7 getestet als User geht es standardmäßig nicht (was zu erwarten war), aber als Admin ohne Fehler.

    Zur Nutzung dieses Forum's, ist ein Übersetzer für folgende Begriffe unerlässlich:

    "On-Bort, weier, verscheiden, schädliges, Butten steyling, näckstet, Parr, Porblem, scripe, Kompletenz, harken, manuel zu extramieren, geckukt, würglich, excell, acces oder Compilevorgeng"

  • Tschuldigung, habe überlesen, dass Du XP benutzt.
    Mein Code läuft erst ab Windows 7 (evtl. auch Vista). Unter XP geht es nicht.

    Aber da ich praktisch keine XP PCs mehr hier habe, kann ich den Code momentan nicht optimieren!

  • Gut, solltest Du vielleicht in die UDF mit reinschreiben. Sonst testet sich noch einer dumm und dusselig !

    Zur Nutzung dieses Forum's, ist ein Übersetzer für folgende Begriffe unerlässlich:

    "On-Bort, weier, verscheiden, schädliges, Butten steyling, näckstet, Parr, Porblem, scripe, Kompletenz, harken, manuel zu extramieren, geckukt, würglich, excell, acces oder Compilevorgeng"

  • Ist soeben eingetragen worden.
    Wenn jemand den Fehler findet, dann kann ich das Script auch wieder für Win XP anpassen.
    Ich habe momentan leider keine XP Maschine mehr hier.

  • Habe im MSDN nochmal nachgesehen:

    Das Task-Scheduler-Object gibt es erst ab Windows Vista.

    Zur Nutzung dieses Forum's, ist ein Übersetzer für folgende Begriffe unerlässlich:

    "On-Bort, weier, verscheiden, schädliges, Butten steyling, näckstet, Parr, Porblem, scripe, Kompletenz, harken, manuel zu extramieren, geckukt, würglich, excell, acces oder Compilevorgeng"

  • Hi veronesi,

    erstmal Danke für diese nette UDF. Mir ist beim überfliegen aufgefallen, dass deine Error Funktion immer 0 zurückgibt und diese Msgbox da theoretisch niemals angezeigt werden dürfte. War das Absicht oder wurde da eine if Bedingung vergessen?

    [autoit]


    Func MyCreateTaskErrFunc()
    $ErrorFound = True
    Return 0

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    MsgBox(0, "AutoItCOM Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _
    ;...

    [/autoit]
  • Also die MsgBox wird nie angezeigt, weil ich in einem automatisierten Script keine Benutzerinteraktionen haben möchte. Es soll alles automatisiert sein. Die war nur für Debugzwecke. Ich habe sie aber drin gelassen, damit man sie jederzeit wieder schnell aktivieren könnte um Fehler zu finden.

    Die Errorfunktion wird nur aufgerufen, wenn ein Fehler aufgetaucht ist, deshalb wird dort dann die Variable gesetzt. Es fehlt also keine If Anweisung.
    Im Prinzip wird also sobald ein beliebiger Fehler auftaucht, nur die Errorvariable gesetzt und dann zurückgesprungen!

    Klar, das könnte man auch noch auf X andere Arten lösen.....
    Gruß Veronesi