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
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
; 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
; 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.
; 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.
; 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.
; 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.
; http://taskscheduler.codeplex.com/
; http://msdn.microsoft.com/en-us/library/…v=VS.85%29.aspx
$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)
; Get a folder to create a task definition in.
$oFolder = $oService.GetFolder("\")
ConsoleWrite("rootFolder " & IsObj($oFolder) & @CRLF)
; 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)
; 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"
; 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
; 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.
$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() = "";'<QueryList><Query Id="0" Path="Application"><Select Path="Application">*[System[Provider[@Name='Application']]]</Select></Query></QueryList>'
; 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.
; 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() = ""
; 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
Global Const $TASK_CREATE_OR_UPDATE = 6
$sUsername = "";@LogonDomain & "\\" & @UserName
$sPassword = ""
$iSecurityDescriptor = ""
;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))
; 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.
; Security Contexts for Tasks
; http://msdn.microsoft.com/en-us/library/…v=vs.85%29.aspx
; http://msdn.microsoft.com/en-us/library/…v=vs.85%29.aspx
;Principal.RunLevel(1) ; Tasks will be run with the highest privileges.
#cs
$sCommand "schtasks.exe /RU " & @UserName & " /SC ONEVENT /TN TestShortcut /TR ""\"""& $sTargetFile & """\"" " & """\""" & $sCommandline & """\"" /IT
/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>
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
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, & @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 _
)
Local $err = $oMyError.number
If $err = 0 Then $err = -1
$g_eventerror = $err ; to check for after this function returns
EndFunc ;==>MyErrFunc