KaFu Posted January 18, 2011 Posted January 18, 2011 Hiho Forum, I've tried to add a task to the task-scheduler using the Task Scheduler Scripting Objects on Win7. I'm not quite what goes wrong, but the last call fails ... anyone willing to take a look how to create a valid task? expandcollapse popup#region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=n #AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator #endregion ;**** Directives created by AutoIt3Wrapper_GUI **** ; http://msdn.microsoft.com/en-us/library/aa383665%28v=VS.85%29.aspx ; Create the TaskService object. $oTaskSchedule = ObjCreate("Schedule.Service") ConsoleWrite("IsObj($oTaskSchedule) " & IsObj($oTaskSchedule) & @CRLF) ConsoleWrite("$oTaskSchedule.service.Connect() " & $oTaskSchedule.Connect() & @CRLF) ; Get a folder to create a task definition in. $rootFolder = $oTaskSchedule.GetFolder("\") ConsoleWrite("rootFolder " & IsObj($rootFolder) & @CRLF) ; The taskDefinition variable is the TaskDefinition object. ; The flags parameter is 0 because it is not supported. $taskDefinition = $oTaskSchedule.NewTask(0) ConsoleWrite("taskDefinition " & IsObj($taskDefinition) & @CRLF) ; Define information about the task. ; Set the registration info for the task by creating the RegistrationInfo object. $regInfo = $taskDefinition.RegistrationInfo() ConsoleWrite("regInfo " & IsObj($regInfo) & @CRLF) $regInfo.Description() = "Start Process as Elevated" $regInfo.Author() = "KaFu" ; Set the principal for the task ; http://msdn.microsoft.com/en-us/library/aa382071%28v=VS.85%29.aspx $principal = $taskDefinition.Principal() ConsoleWrite("principal " & IsObj($principal) & @CRLF) $principal.LogonType() = 3 ; TASK_LOGON_INTERACTIVE_TOKEN, User must already be logged on. The task will be run only in an existing interactive session. $principal.RunLevel() = 1 ; Tasks will be run with the highest privileges. ; Set the task setting info for the Task Scheduler by creating a TaskSettings object. ; http://msdn.microsoft.com/en-us/library/aa382542%28v=VS.85%29.aspx $settings = $taskDefinition.Settings() ConsoleWrite("settings " & IsObj($settings) & @CRLF) $settings.Enabled() = True ; The task can be performed only when this setting is True. $settings.Priority() = 5 ; NORMAL_PRIORITY_CLASS $settings.AllowDemandStart() = 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. $settings.StartWhenAvailable() = True $settings.Hidden() = False $settings.MultipleInstances() = 0 ; Starts a new instance while an existing instance of the task is running. $settings.ExecutionTimeLimit() = "" ; When this parameter is set to Nothing, the execution time limit is infinite. ; Create triggers $triggers = $taskDefinition.Triggers() ConsoleWrite("triggers " & IsObj($triggers) & @CRLF) ; Trigger Object ; http://msdn.microsoft.com/en-us/library/aa383868%28v=VS.85%29.aspx ; A maximum of 500 tasks with event subscriptions can be created. $trigger = $triggers.Create(0) ; TASK_TRIGGER_EVENT; http://msdn.microsoft.com/en-us/library/aa383898%28v=VS.85%29.aspx ConsoleWrite("trigger " & IsObj($trigger) & @CRLF) $trigger.Enabled() = True ; Create the action for the task to execute. ; Add an action to the task to run notepad.exe. ; http://msdn.microsoft.com/en-us/library/aa446808%28v=VS.85%29.aspx $Action = $taskDefinition.Actions.Create(0) ; TASK_ACTION_EXEC ConsoleWrite("Action " & IsObj($Action) & @CRLF) $Action.ID = "Boot Task Test" $Action.Path() = "C:\Windows\System32\notepad.exe" $Action.WorkingDirectory() = "C:\Windows\System32" $Action.Arguments() = "" ; Register (create) the task. ;http://msdn.microsoft.com/en-us/library/aa382577%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 $rootFolder.RegisterTaskDefinition("TestElevateProcess", $taskDefinition, 6, "", "", 3) ; http://msdn.microsoft.com/en-us/library/aa446890%28v=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/aa382140%28v=vs.85%29.aspx ; http://msdn.microsoft.com/en-us/library/aa382076%28v=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 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” 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 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2025-May-18) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now