Link to home
Start Free TrialLog in
Avatar of beapit
beapit

asked on

Launchapp.vbs to run on battery power

I'm using the lauchapp.vbs script to run a login script in the user context for Windows 7 machines. I ran into a problem with it when a laptop is on battery power. I've discovered that the scheduled task that the launchapp.vbs script creates has the check boxes checked for "Start the task only if the computer is on AC Power" and the sub category "Stop if the computer switches to battery power" under the Conditions tab of the task.

Just hoping someone knows how to create a scheduled task without those two options checked. I've attached the relevant part of the script.
Sub LaunchApp

  Dim objTaskService
  Dim strTaskName, rootFolder, taskDefinition, triggers, trigger, Action

  'Create the TaskService object
  Set objTaskService = CreateObject("Schedule.Service")
  Call objTaskService.Connect()
  strTaskName = "Launch App As Interactive User"

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

  'Delete the task if already present
  On Error Resume Next
  Call rootFolder.DeleteTask(strTaskName, 0)
  Err.Clear

  'Create the new task
  Set taskDefinition = objTaskService.NewTask(0)

  'Create a registration trigger
  Set triggers = taskDefinition.Triggers
  Set trigger = triggers.Create(TriggerTypeRegistration)

  'Create the action for the task to execute
  Set Action = taskDefinition.Actions.Create(ActionTypeExecutable)
  Action.Path = strKIX
  Action.Arguments = strScriptName
  Action.WorkingDirectory = strWorkingDirectory

  'Register (create) the task
  call rootFolder.RegisterTaskDefinition(strTaskName, taskDefinition, FlagTaskCreate,,, LogonTypeInteractive)

  Set objTaskService = nothing

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Lee Osborne
Lee Osborne
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of beapit
beapit

ASKER

What part would I set that on? objTaskService?

E.g.

objTaskService.StopIfGoingOnBatteries = "False"
objTaskService.DisallowStartIfOnBatteries = "False"

Sorry, I'm not very knowledgeable with VBScript.

Thanks
Kevin
No problem.

I 'think' you might set it with Action.StopIfGoingOnBatteries = False

I'm not 100% on that, and unless somebody can confirm in the meantime, I'll get back to you. Otherwise, give it a try!

Lee
Avatar of beapit

ASKER

I tried writing it several different ways and even used an example from http://rwandering.net/2008/02/28/unprivileged-tasks-in-windows-6-with-vbs/ and nothing worked.

Then I tried just putting it in like this and it worked! Thanks for your help. Working code is attached.

taskDefinition.Settings.DisallowStartIfOnBatteries = false
taskDefinition.Settings.StopIfGoingOnBatteries = false
Sub LaunchApp

  Dim objTaskService
  Dim strTaskName, rootFolder, taskDefinition, triggers, trigger, Action

  'Create the TaskService object
  Set objTaskService = CreateObject("Schedule.Service")
  Call objTaskService.Connect()
  strTaskName = "Launch App As Interactive User0"

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

  'Delete the task if already present
  On Error Resume Next
  Call rootFolder.DeleteTask(strTaskName, 0)
  Err.Clear

  'Create the new task
  Set taskDefinition = objTaskService.NewTask(0)
  taskDefinition.Settings.DisallowStartIfOnBatteries = false
  taskDefinition.Settings.StopIfGoingOnBatteries = false
    
  'Create a registration trigger
  Set triggers = taskDefinition.Triggers
  Set trigger = triggers.Create(TriggerTypeRegistration)

  'Create the action for the task to execute
  Set Action = taskDefinition.Actions.Create(ActionTypeExecutable)
  Action.Path = strKIX
  Action.Arguments = strScriptName
  Action.WorkingDirectory = strWorkingDirectory
  
  'Register (create) the task
  call rootFolder.RegisterTaskDefinition(strTaskName, taskDefinition, FlagTaskCreate,,, LogonTypeInteractive)

  Set objTaskService = nothing

End Sub

Open in new window

Avatar of beapit

ASKER

He pointed me in the right direction but I had to dig to find what I actually needed to do.
Excellent, glad you found the final solution!

Lee