Jump to content
Sign in to follow this  

Create Task Scheduler Scripting Objects

Recommended Posts

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 :x ... anyone willing to take a look how to create a valid task?

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#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.
    Function RegisterTaskDefinition( _
    ByVal path, _
    ByVal definition, _
    ByVal flags, _
    ByVal userId, _
    ByVal password, _
    ByVal longonType, _
    [ ByVal sddl ], _
    ByRef task _
$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.

    $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
    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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

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

Create an account

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

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...