Launchapp.vbs to run on battery power

beapit
beapit used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Infrastructure Engineer
Commented:
I think you can set False to StopIfGoingOnBatteries and DisallowStartIfOnBatteries

http://msdn.microsoft.com/en-us/library/aa383607.aspx

Lee

Author

Commented:
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
Lee OsborneSenior Infrastructure Engineer

Commented:
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
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Author

Commented:
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

Author

Commented:
He pointed me in the right direction but I had to dig to find what I actually needed to do.
Lee OsborneSenior Infrastructure Engineer

Commented:
Excellent, glad you found the final solution!

Lee

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial