44

I have a VBScript script that takes 2 command-line arguments and does some validation. I need to debug this to see how the program is getting executed. I was trying to paste this into Excel (using VBA). However there are some constructs like Const, etc. that are not being supported, plus certain validations like taking the command-line arguments are failing.

Hence I would like to know if there is any way to debug this script. I have Windows XP and Office 2003 installed. I cannot install any other applications on the office computer.

STF
  • 1,485
  • 3
  • 19
  • 36
Dheer
  • 3,926
  • 6
  • 34
  • 45
  • **Note:** There is also an answer regarding VBScript debugging with Visual Studio, you can find it [here](https://stackoverflow.com/q/23210905/1016343). – Matt Feb 13 '20 at 10:24

5 Answers5

54

Run cscript.exe for full command args, I think

cscript //X scriptfile.vbs MyArg1 MyArg2

will run the script in a debugger.

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • I am getting an error cscript //X test.vbs ... CScript Error: Initialization of the Windows Script Host failed. (Access is denied.) must be something to do with priviliges on my computer. – Dheer Feb 18 '10 at 11:50
  • 8
    You need administrative access to run a debugger. – Tmdean Feb 18 '10 at 14:36
  • 1
    I get a different problem. I select Visual Studio 2012 as a debugger, then the IDE opens with a solution called "cscript" containing a "cscript" document, but that's all, my script does not appear nowhere. – wip Mar 11 '14 at 06:37
  • 5
    I just wanted to add that if you use the Cscript //X method and no debugger comes up (even though Microsoft Script Debugger is installed), you probably have to add a registry key that selects the debugger. [Source](http://support.microsoft.com/kb/308364) Save the following code in a reg file and point the path to your debugger. It does not have to be the full path `Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\CLSID\{834128A2-51F4-11D0-8F20-00805F2CD064}\LocalServer32] @="C:\\Program Files (x86)\\Microsoft Script Debugger\\msscrdbg.exe"` – Mr_Moneybags May 14 '14 at 06:01
  • This immediately ran my VBS script without giving me a chance to set breakpoints or inspect variables. – Stevoisiak Oct 24 '17 at 18:28
  • 3
    @wil As is, the debugger will only halt the script if there is an error. If you pass the `//D` option on the command line, the script break at the first line, without waiting for an unhandled error. – Zev Spitz May 10 '18 at 07:38
5

For posterity, here's Microsoft's article KB308364 on the subject. This no longer exists on their website, it is from an archive.

How to debug Windows Script Host, VBScript, and JScript files

SUMMARY

The purpose of this article is to explain how to debug Windows Script Host (WSH) scripts, which can be written in any ActiveX script language (as long as the proper language engine is installed), but which, by default, are written in VBScript and JScript. There are certain flags in the registry and, depending on the debugger used, certain required procedures to enable debugging.

MORE INFORMATION

To debug WSH scripts in Microsoft Visual InterDev, the Microsoft Script Debugger, or any other debugger, use the following command-line syntax to start the script:

wscript.exe //d <path to WSH file>

This code informs the user when a runtime error has occurred and gives the user a choice to debug the application. Also, the //x flag can be used, as follows, to throw an immediate exception, which starts the debugger immediately after the script starts running:

wscript.exe //d //x <path to WSH file>

After a debug condition exists, the following registry key determines which debugger will be used:

HKEY_CLASSES_ROOT\CLSID\{834128A2-51F4-11D0-8F20-00805F2CD064}\LocalServer32

The script debugger should be Msscrdbg.exe, and the Visual InterDev debugger should be Mdm.exe.

If Visual InterDev is the default debugger, make sure that just-in-time (JIT) functionality is enabled. To do this, follow these steps:

  1. Start Visual InterDev.

  2. On the Tools menu, click Options.

  3. Click Debugger, and then ensure that the Just-In-Time options are selected for both the General and Script categories.

Additionally, if you are trying to debug a .wsf file, make sure that the following registry key is set to 1:

HKEY_CURRENT_USER\Software\Microsoft\Windows Script\Settings\JITDebug

PROPERTIES

Article ID: 308364 - Last Review: June 19, 2014 - Revision: 3.0

Keywords: kbdswmanage2003swept kbinfo KB308364

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
4

This is for future readers. I found that the simplest method for me was to use Visual Studio -> Tools -> External Tools. More details in this answer.

Easier to use and good debugging tools.

shash
  • 246
  • 1
  • 7
3

Click the mse7.exe installed along with Office typically at \Program Files\Microsoft Office\OFFICE11.

This will open up the debugger, open the file and then run the debugger in the GUI mode.

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Dheer
  • 3,926
  • 6
  • 34
  • 45
-1

Export this folder to a backup file and try remove this folder and all the content.

HKEY_CURRENT_USER\Software\Microsoft\Script Debugger
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225