Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to modify this script to add this two functions :
1- run with highest privileges
2- the power option there is battery and ac. How to make it run on both? If this is hard okay, it's not important
VB
' This sample schedules a task to start notepad.exe when a user logs on.
'---------------------------------------------------------

' A constant that specifies a logon trigger.
const TriggerTypeLogon = 9
' A constant that specifies an executable action.
const ActionTypeExecutable = 0   

const TASK_LOGON_INTERACTIVE_TOKEN = 3

'********************************************************
' Create the TaskService object.
Set service = CreateObject("Schedule.Service")
call service.Connect()

'********************************************************
' Get a folder to create a task definition in. 
Dim rootFolder
Set rootFolder = service.GetFolder("\")

' The taskDefinition variable is the TaskDefinition object.
Dim taskDefinition
' The flags parameter is 0 because it is not supported.
Set taskDefinition = service.NewTask(0) 

'********************************************************
' Define information about the task.

' Set the registration info for the task by 
' creating the RegistrationInfo object.
Dim regInfo
Set regInfo = taskDefinition.RegistrationInfo
regInfo.Description = "Task will execute Notepad when a " & _
    "specified user logs on."
regInfo.Author = "Author Name"

' Set the task setting info for the Task Scheduler by
' creating a TaskSettings object.
Dim settings
Set settings = taskDefinition.Settings
settings.StartWhenAvailable = True

'********************************************************
' Create a logon trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeLogon)


trigger.ExecutionTimeLimit = "PT5M"    ' Five minutes
trigger.Id = "LogonTriggerId"
trigger.UserId = ""   ' Must be a valid user account   

'***********************************************************
' Create the action for the task to execute.

' Add an action to the task. The action executes notepad.
Dim Action
Set Action = taskDefinition.Actions.Create( ActionTypeExecutable )
Action.Path = "C:\Windows\System32\notepad.exe"

WScript.Echo "Task definition created. About to submit the task..."

'***********************************************************
' Deal with the password problems

taskDefinition.Principal.UserId = ""
taskDefinition.Principal.LogonType = TASK_LOGON_INTERACTIVE_TOKEN

'***********************************************************
' Register (create) the task.
const createOrUpdateTask = 6
call rootFolder.RegisterTaskDefinition( _
    "Test Logon Trigger", taskDefinition, createOrUpdateTask, _
    Empty , Empty, TASK_LOGON_INTERACTIVE_TOKEN)

WScript.Echo "Task submitted."
Posted
Updated 4-Jan-16 0:49am
v2

1 solution

You need to add here "taskDefinition.Principal.RunLevel" and set it to

' TASK_RUNLEVEL_TYPE
Const TASK_RUNLEVEL_LUA = 0
Const TASK_RUNLEVEL_HIGHEST = 1

IMPORTANT: If you use Highest Privilege, the script will need to run elevated!

VB
'***********************************************************
' Deal with the password problems

taskDefinition.Principal.UserId = ""
taskDefinition.Principal.LogonType = TASK_LOGON_INTERACTIVE_TOKEN

taskDefinition.Principal.RunLevel = TASK_RUNLEVEL_HIGHEST ' TASK_RUNLEVEL_TYPE
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900