如何调试独立的VBScript脚本?

44
我有一个VBScript脚本,它需要2个命令行参数并进行一些验证。我需要调试它以查看程序的执行情况。我试图将其粘贴到Excel中(使用VBA)。但是,有一些构造不被支持,例如Const等,并且某些验证,例如获取命令行参数失败。因此,我想知道是否有任何方法可以调试此脚本。我安装了Windows XP和Office 2003。我无法在办公电脑上安装其他应用程序。

注意:这里还有一个关于使用Visual Studio进行VBScript调试的答案,您可以在此处找到它(https://dev59.com/kmAg5IYBdhLWcg3wwtRp)。 - Matt
5个回答

54

我认为需要使用cscript.exe来获取完整的命令参数。

cscript //X scriptfile.vbs MyArg1 MyArg2

使用调试器运行脚本。


我遇到了一个错误 cscript //X test.vbs ... CScript 错误: Windows 脚本宿主的初始化失败。(访问被拒绝。) 这可能与我的计算机权限有关。 - Dheer
8
您需要管理员权限才能运行调试器。 - Tmdean
1
我遇到了一个不同的问题。我选择Visual Studio 2012作为调试器,然后IDE打开了一个名为"cscript"的解决方案,其中包含一个"cscript"文档,但是我的脚本没有出现在任何地方。 - wip
5
如果你使用 Cscript //X <filename> 方法时,没有调试器弹出(即使已安装 Microsoft Script Debugger),那么你可能需要添加一个选择调试器的注册表键值。将以下代码保存为 reg 文件,并将路径指向你的调试器,它不必是完整路径: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
这会立即运行我的VBS脚本,而不给我设置断点或检查变量的机会。 - Stevoisiak
3
如果不加//D这个命令行选项,调试器只会在出现错误时中断脚本。如果加上//D选项,则脚本会在第一行就中断,而不必等待未处理的错误出现。 - Zev Spitz

5

这段内容是给未来的读者看的。我发现对我来说最简单的方法是使用Visual Studio -> 工具 -> 外部工具。 更多详细信息请参考这个答案

这种方法使用起来更加容易,并且拥有良好的调试工具。


5

为后人留存,以下是与此主题相关的微软文章KB308364。该文章已不再存在于他们的网站上,它来自一个归档

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


3

点击通常安装在\Program Files\Microsoft Office\OFFICE11路径下的mse7.exe

这将打开调试器,打开文件并以GUI模式运行调试器。


-1

将此文件夹导出为备份文件,然后尝试删除此文件夹及其所有内容。

HKEY_CURRENT_USER\Software\Microsoft\Script Debugger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接