0

I have troubles with the task scheduler in windows 7 Pro. I want to run a script. Manual execution works perfectely, the task manager does nothing.

Part of the script is setting the focus onto a specific window using this VBScript:

Dim ObjShell :Set ObjShell = CreateObject("Wscript.Shell")
ObjShell.AppActivate("Notepad")

The 2 lines above are only part of the bigger script, but for error checking I excluded everything else. First I have to get that running then the original.

Notepad is open of course and manually executing the vbs sets the focus. Running the script using task scheduler does not set the focus. I even ran the script manually in task scheduler (rightclick - run) - does not set focus.

The task is run with elevated privileges and with the same user as manual start. The computer is not shut down, not locked.

Why does the script run manually but not in task scheduler? How can I get it to run in task scheduler?

€dit: I want to run the vbs file, not a .bat file if possible, as I do not want a cmd window to open.

  • 1
    Try changing it to cscript.exe and see if it will execute. – NetworkKingPin Jun 6 '16 at 11:45
  • I tried, it does not :( I created another .bat file with cscript.exe script.vbs inside, but it does not run either :( – eckhart Jun 6 '16 at 11:50
2

In order for a task to have access to the interactive session (i.e. running programs and open windows), you need to configure it to run under a logged on user. Simply go to the task properties and in the General tab select Run only when user is logged on.

Task properties dialog

Using the other option is more secure, but that makes it more restrictive.

|improve this answer|||||
0

AppActivate doesn't work from a Task; most likely as a security precaution.

The general advice around the 'Net (assuming you then want to use SendKeys and alike) is to give up and use AutoIt instead of VBScript.

PS: I also tried PowerShell with SetForegroundWindow (imported from user32.dll), and it behaved the same way as VBS.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.