|
'------------------------------------------------------------------ |
|
' This sample schedules a task to start on a daily basis. |
|
'------------------------------------------------------------------ |
|
|
|
Const TASK_TRIGGER_EVENT = 0 ' Starts the task when a specific event occurs. |
|
Const TASK_TRIGGER_TIME = 1 ' Starts the task at a specific time of day. |
|
Const TASK_TRIGGER_DAILY = 2 ' Starts the task daily. |
|
Const TASK_TRIGGER_WEEKLY = 3 ' Starts the task weekly. |
|
Const TASK_TRIGGER_MONTHLY = 4 ' Starts the task monthly. |
|
Const TASK_TRIGGER_MONTHLYDOW = 5 ' Starts the task every month on a specific day of the week. |
|
Const TASK_TRIGGER_IDLE = 6 ' Starts the task when the computer goes into an idle state. |
|
Const TASK_TRIGGER_REGISTRATION = 7 ' Starts the task when the task is registered. |
|
Const TASK_TRIGGER_BOOT = 8 ' Starts the task when the computer boots. |
|
Const TASK_TRIGGER_LOGON = 9 ' Starts the task when a specific user logs on. |
|
Const TASK_TRIGGER_SESSION_STATE_CHANGE = 11 ' Triggers the task when a specific sessio |
|
|
|
Const TASK_ACTION_EXEC = 0 ' This action performs a command-line operation. For example, the action could run a script, launch an executable, or, if the name of a document is provided, find its associated application and launch the application with the document. |
|
Const TASK_ACTION_COM_HANDLER = 5 ' This action fires a handler. |
|
Const TASK_ACTION_SEND_EMAIL = 6 ' This action sends an email message. |
|
Const TASK_ACTION_SHOW_MESSAGE = 7 ' This action shows a message box. |
|
|
|
Const TASK_LOGON_NONE = 0 'The logon method is not specified. Used for non-NT credentials. |
|
Const TASK_LOGON_PASSWORD = 1 'Use a password for logging on the user. The password must be supplied at registration time. |
|
Const TASK_LOGON_S4U = 2 'Use an existing interactive token to run a task. The user must log on using a service for user (S4U) logon. When an S4U logon is used, no password is stored by the system and there is no access to either the network or encrypted files. |
|
Const TASK_LOGON_INTERACTIVE_TOKEN = 3 'User must already be logged on. The task will be run only in an existing interactive session. |
|
Const TASK_LOGON_GROUP = 4 'Group activation. The userId field specifies the group. |
|
Const TASK_LOGON_SERVICE_ACCOUNT = 5 'Indicates that a Local System, Local Service, or Network Service account is being used as a security context to run the task. |
|
Const TASK_LOGON_INTERACTIVE_TOKEN_OR_PASSWORD = 6 'First use the interactive token. If the user is not logged on (no interactive token is available), then the password is used. The password must be specified when a task is registered. This flag is not recommended for new tasks because it is less reliable than TASK_LOGON_PASSWORD. |
|
|
|
cONST TASK_RUNLEVEL_LUA = 0 'Tasks will be run with the least privileges (LUA). |
|
cONST TASK_RUNLEVEL_HIGHEST = 1 'Tasks will be run with the highest privileges. |
|
|
|
'******************************************************** |
|
' 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. |
|
|
|
' ' Setting principal - it should run even if user is not logged in |
|
' Dim principal |
|
' set principal = taskDefinition.principal |
|
' principal.LogonType = TASK_LOGON_PASSWORD |
|
' ' principal.password = "unciBal7791" |
|
' ' principal.UserId = "nikola" |
|
' ' principal.RunLevel = TASK_RUNLEVEL_HIGHEST |
|
|
|
' Set the registration info for the task by |
|
' creating the RegistrationInfo object. |
|
Dim regInfo |
|
Set regInfo = taskDefinition.RegistrationInfo |
|
regInfo.Description = "Logs time spent on battery. For more info see: https://gist.github.com/knee-cola/49a7cedf3675da485e76d6e1e5e838c6" |
|
regInfo.Author = "Administrator" |
|
|
|
' Set the task setting info for the Task Scheduler by |
|
' creating a TaskSettings object. |
|
Dim settings |
|
Set settings = taskDefinition.Settings |
|
settings.Enabled = True |
|
settings.StartWhenAvailable = True |
|
settings.Hidden = False |
|
' we need to have task running while on batteries |
|
settings.StopIfGoingOnBatteries = False |
|
settings.DisallowStartIfOnBatteries = False |
|
|
|
'******************************************************** |
|
' Create a daily trigger. Note that the start boundary |
|
' specifies the time of day that the task starts and the |
|
' interval specifies what days the task is run. |
|
Dim triggers |
|
Set triggers = taskDefinition.Triggers |
|
|
|
Dim trigger |
|
Set trigger = triggers.Create(TASK_TRIGGER_DAILY) |
|
|
|
' Trigger variables that define when the trigger is active |
|
' and the time of day that the task is run. The format of |
|
' this time is YYYY-MM-DDTHH:MM:SS |
|
|
|
Const startTime = "2006-05-02T08:00:00" 'Task runs at 8:00 AM |
|
Const endTime = "2099-05-02T08:00:00" |
|
|
|
trigger.StartBoundary = startTime |
|
trigger.EndBoundary = endTime |
|
trigger.DaysInterval = 1 'Task runs every day. |
|
trigger.Id = "DailyTriggerId" |
|
trigger.Enabled = True |
|
|
|
' Set the task repetition pattern for the task. |
|
Dim repetitionPattern |
|
Set repetitionPattern = trigger.Repetition |
|
' repetitionPattern.Duration = "PT4M" - it will be repeated indefinitely |
|
repetitionPattern.Interval = "PT1M" ' repear the task once every minute |
|
|
|
' Creating an additional trigger, which will get called each time |
|
' a charger was plugged in or unplugged |
|
Set trigger = triggers.Create(TASK_TRIGGER_EVENT) |
|
|
|
trigger.Id = "EventTriggerId" 'Inherited from the Trigger object. Gets or sets the identifier for the trigger. |
|
' trigger.Delay = 0 'Gets or sets a value that indicates the amount of time between when the event occurs and when the task is started. |
|
trigger.Enabled = true 'Inherited from the Trigger object. Gets or sets a Boolean value that indicates whether the trigger is enabled. |
|
' trigger.StartBoundary = startTime 'Inherited from the Trigger object. Gets or sets the date and time when the trigger is activated. |
|
' trigger.EndBoundary = endTime 'Inherited from the Trigger object. Gets or sets the date and time when the trigger is deactivated. The trigger cannot start the task after it is deactivated. |
|
trigger.Subscription = "<QueryList><Query Id='0' Path='System'><Select Path='System'>*[System[EventID=105]]</Select></Query></QueryList>" 'Gets or sets the XPath query string that identifies the event that fires the trigger. |
|
' trigger.ExecutionTimeLimit = "PT5M" 'Inherited from the Trigger object. Gets or sets the maximum amount of time that the task launched by this trigger is allowed to run. |
|
' trigger.Repetition = 'Inherited from the Trigger object. Gets or sets how often the task is run and how long the repetition pattern is repeated after the task is started. |
|
' trigger.ValueQueries = 'Gets or sets a collection of named XPath queries. Each query in the collection is applied to the last matching event XML that is returned from the subscription query specified in the Subscription property. The name of the query can be used as a variable in the message of a ShowMessageAction action. |
|
|
|
'*********************************************************** |
|
' Create the action for the task to execute. |
|
|
|
' Add an action to the task to run notepad.exe. |
|
Dim Action |
|
Set Action = taskDefinition.Actions.Create( TASK_ACTION_EXEC ) |
|
'Action.Path = "cscript" |
|
'Action.Arguments = "battery-worker.vbs" |
|
Action.Path = "battery-worker.vbs" |
|
Action.WorkingDirectory = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\" |
|
|
|
'*********************************************************** |
|
' Register (create) the task. |
|
|
|
' https://msdn.microsoft.com/en-us/library/windows/desktop/aa382577(v=vs.85).aspx |
|
|
|
Set oWshShell = WScript.CreateObject("WScript.Shell") |
|
|
|
Set objWSHshell = WScript.CreateObject( "WScript.Shell" ) |
|
|
|
Const path = "BatteryTrackerWorker" |
|
Const flags = 6 |
|
Dim userId |
|
userId = objWSHshell.ExpandEnvironmentStrings( "%USERNAME%" ) |
|
Dim password |
|
password = InputBox("Please provide your system password." & Chr(10) & "It's needed to register the task in Task Scheduler.", "BatteryTracker Installation") |
|
|
|
Dim logonType |
|
logonType = TASK_LOGON_PASSWORD |
|
|
|
call rootFolder.RegisterTaskDefinition(path, taskDefinition, flags, userId, password, logonType) |
|
|
|
WScript.Echo "Battery Task created!" & Chr(10) & "Installation completed successfully!" & Chr(10) & Chr(10) & "You cand find it in Task Scheduler under " & path |