Running Visual Basic Script as a Scheduled Task asks how I want to Open the File

21,785

Solution 1

It sounds like you have wscript configured as the default association for .vbs files instead of cscript. You can check at a command prompt, using ftype. On my plain Win7 64-bit Pro machine, it produces the following (which would indicate mine is configured to wscript.exe as well):

C:\>ftype VBSFile
VBSFile="%SystemRoot%\System32\WScript.exe" "%1" %*

Change the Program/script entry, and add cscript.exe to the beginning, so the full entry would read (all on one line, of course):

cscript.exe "C:\Program Files (x86)\Malwarebytes' Anti-Malware\Malwarebytes Task Scheduler Instructions\Batch Files\MalwarebytesScanAndUpdate.vbs"

If the Task Scheduler complains about cscript, change it to

"%SystemRoot%\System32\cscript.exe" "C:\Program Files (x86)\Malwarebytes' Anti-Malware\Malwarebytes Task Scheduler Instructions\Batch Files\MalwarebytesScanAndUpdate.vbs"

If this doesn't work, cut the full pathname (again, including quotes), and paste it into the edit control labeled Add arguments (optional):.

You can also set cscript to be your default script processor at the command-line (although I'd discourage it unless you're sure that's what you want to do):

C:\>cscript /H:Cscript

Note you'll have to do so at a command prompt opened as an administrator ("Start", type "Command", right click on "Command prompt" and choose "Run as Administrator" from the context menu), and execute the above from there. You'll get something like this as the output:

C:\Windows\system32>cscript /H:CScript
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

The default script host is now set to "cscript.exe".

If you decide to undo it, repeat the above and use /H:WScript instead.

C:\Windows\system32>cscript /H:WScript
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

The default script host is now set to "wscript.exe".

Do they really include a single quote in their folder name? That's pretty nasty, IMO. :-)

Solution 2

I know this is really old, but I just had the same problem and solved it in the following way, whether or not it's the "correct" way, other's can judge.

I was trying to schedule a script C:\reminder.vbs to run at a specific time and had the same issue where I could run it by double clicking in explorer no worries, but when the task ran it asked what I wanted to open it with. I resolved the issue by changing the scheduled program/script from C:\reminder.vbs to wscript.exe "C:\reminder.vbs"

So it perhaps doesn't really explain why the initial problem was occurring, but it does make the script run correctly.

Solution 3

This happened to me as well, on a fairly "clean" Windows Server 2012 R2 system, with only Firefox and WinRAR installed in addition to various Windows roles and features.

The scheduled task was set to run something like:

"C:\Program Files (x86)\example\Example\Sample App\Directory\example.vbs"

The above was the result of selecting the .vbs with the Browse function of the Task Scheduler Properties dialog.

Running the task manually with the "[x] Run only when user is logged on" option opens the "How do you want to open this file?", prompting to select one of Firefox, IE, Notepad, etc.

Note that without the above setting, it only silently gives "The operation was canceled by the user. (0x800704C7)" Last Run Result. So I hope that other people looking for this error can now find this thread and try the "[x] Run only when user is logged on" option to see if they have this exact problem.

assoc .vbs gives: .vbs=VBSFile

ftype VBSFile gives: VBSFile="%SystemRoot%\System32\WScript.exe" "%1" %*

I tried setting the current directory in the "Start in" option, but that did not help either.

The solution in my case was to not simply add the current directory, but to also edit the Program/script string that the Browse button had set.

This:

"C:\Program Files (x86)\example\Example\Sample App\Directory\example.vbs"

had to become:

example.vbs

And the "optional" Start in field had to be set to:

C:\Program Files (x86)\example\Example\Sample App\Directory\

Other variations, e.g. the path with quotes, or Program/script with the path, did not resolve.

Solution 4

You say that you're prompted how you want to open the (VBScript?) file, but the screenshot of the error message you posted says that your VBScript cannot find the batch script it's trying to run. These are two different issues and not related to each other.

Prompts about how you want to open a particular file are usually caused by a missing association of the file's extension with an executable program, or by a missing default action.

This might indicate malware on your system, because by default the extension .vbs is associated with wscript.exe.

You can either use assoc and ftype to verify the association:

C:\>assoc .vbs
.vbs=VBSFile

C:\>ftype vbsfile
vbsfile="%SystemRoot%\System32\WScript.exe" "%1" %*

or you can directly check the respective registry settings, which should look like this:

HKEY_CLASSES_ROOT
+-.vbs : (Default)  REG_SZ  "VBSFile"
|
`-VBSFile
  `-Shell : (Default)  REG_SZ  "Open"
    `-Open
      `-Command : (Default)  REG_EXPAND_SZ  "%SystemRoot%\System32\WScript.exe" "%1" %*

The error message from your screenshot

The system cannot find the file specified.

is generated, because your VBScript cannot find the batch file MalwarebytesUpdate.bat in the current working directory. There are three ways to fix this:

  1. Set the working directory of the scheduled task (field Start in (optional):) to the folder containing the batch file.

  2. Have your VBScript change the working directory to the folder containing the batch file.

    shell.CurrentDirectory = "C:\Program F...ructions\Batch Files"
    shell.Run "MalwarebytesUpdate.bat"
    
  3. Specify the full path to the batch file in the VBScript.

    shell.Run """C:\Program F...ructions\Batch Files\MalwarebytesUpdate.bat"""
    

    Note that in this case you need double quotes around the path, because it contains spaces!

Share:
21,785
DaveTheMinion
Author by

DaveTheMinion

I ♥ minions.

Updated on July 27, 2020

Comments

  • DaveTheMinion
    DaveTheMinion almost 4 years

    I have created a task in the Task Scheduler that is supposed to execute a .vbs file that I wrote. The problem is that each time the service runs, rather than actually executing the code, I am asked how I want to open the file. How can I stop this? Please do not suggest batch files; I cannot use them for this.

    enter image description here

    The command line is "C:\Program Files (x86)\Malwarebytes' Anti-Malware\Malwarebytes Task Scheduler Instructions\Batch Files\MalwarebytesScanAndUpdate.vbs".

    The vbs file's code is...

    Dim shell
    Set shell = WScript.CreateObject ("WScript.Shell")
    shell.Run "MalwarebytesUpdate.bat"
    WScript.Sleep 300000 'Sleeps for 5 minutes.
    shell.Run "MalwarebytesScan.bat"
    

    Error enter image description here

  • Ken White
    Ken White over 10 years
    The question says he's already done this, and it isn't working properly. There's even an image now.
  • DaveTheMinion
    DaveTheMinion over 10 years
    I have already done all of this. The problem is that when the task starts, rather than actually running it, it just asks me how I would like to open the file. I am trying to use a vbs file instead of a batch file because I do not want any consoles to be visible when the task is running.
  • DaveTheMinion
    DaveTheMinion over 10 years
    Using the entry you gave me, I get a message saying that the system cannot find the specified file. As for your suggestion about moving the filepath into the argument box, the scheduler did it automatically. If you need, I will post a screenshot of the entire error.
  • Ken White
    Ken White over 10 years
    Sorry. Did you copy/paste? There's an erroneous space (typo) between cscript. and exe. I'll fix it (as well as posting an alternative means of specifying it).
  • DaveTheMinion
    DaveTheMinion over 10 years
    No, I copied the one above that did not have the extra space between cscript. and exe.
  • Ken White
    Ken White over 10 years
    :-) I just corrected it, so if you're seeing it without the space you're seeing the correction. Check the entry again in Task Scheduler. I also added an alternative that includes the full path (the one with %SystemRoot% in it).
  • Ken White
    Ken White over 10 years
    Based on the command prompt capture you included, you're missing the double-quotes around the path to the script file. Check the path and filename to the script very carefully in the Task Scheduler dialog. If Windows can't find it, you've missed something.
  • DaveTheMinion
    DaveTheMinion over 10 years
    Now that I've added those double-quotes, I am getting an error saying There is no file extension in C:\Program.
  • Ken White
    Ken White over 10 years
    I can't physically view your screen to see what the problem is; you're going to have to do that yourself. You have something wrong with the path and filename to the script file. If you can't figure it out, clear it and start over, or delete the task and start from scratch. I can't debug remotely on your system; you're going to have to take it upon yourself to carefully review what you've entered and find the mistake.
  • DaveTheMinion
    DaveTheMinion over 10 years
    Okay, thank you! I will let you know what the end result is when I have fixed the problem. Thanks for all of your help. I will come back and accept your answer A.S.A.P. For the record, I just checked, and the filepath is correct. I will look over it later though.
  • Ansgar Wiechers
    Ansgar Wiechers over 10 years
    There's nothing wrong with single quotes in file or folder names on Windows, since single quotes aren't quoting characters.
  • DaveTheMinion
    DaveTheMinion over 10 years
    Thank you for pointing out that I needed to add shell.CurrentDirectory. I assumed that I would be okay without it because the batch files are in the same folder as the script, but apparently it does not work that way.
  • Ken White
    Ken White over 10 years
    @AnsgarWiechers: I didn't say there wasy anything wrong with it. I said it was ugly.
  • G-Wiz
    G-Wiz over 8 years
    Epic, worked for me. Thanks for submitting this answer.