We help IT Professionals succeed at work.

Debugging a vbscript in a WSF file

zc2
zc2 used Ask the Experts™
on
I have a code on vbscript, I am trying to debug it by tracing with Visual Studio. I start it with a command
cscript //D myscript.vbs

Open in new window

When a stop command is executed or an error happened I attach the VS.
The problem here that it only allows me to put a breakpoint to certain lines of the code (always to the one where the stop was, and some else, I did not get the rules), making the debugging process to be very frustrating.
When I'm trying to do the "Set the next statement" command to move the execution pointer (I press Ctrl+Shift+F10) to a line where it previously did not let me to put a breakpont, I got a message box, saying "Unable to set the next statement. There is no execution code at this location in the source code."

Is that a normal behaviour or something is misconfigured?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Not sure if this will help, I typically use /X rather than /D for debugging VBS in VS.  For example:

cscript /x c:\temp\test.vbs

This should open the script up in VS (after prompting you to pick your debugger) and then allow you to set breakpoints where ever you want them.

Note also that you only need to double up on the / if the /x comes after the script file path, if you place it before like I did then a single / is fine.


»bp

Author

Commented:
Having //X instead of //D is just the same as placing the "stop" at the very first line of the script. But I tried that, still it does not allow me to put a breakpoint to every line I like.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Can you share a very short example VBS script where this occurs?  It may be something in the way the code is structured, etc.

Here's a simple little test script I ran here and it allowed me to set a breakpoint on each line as shown below.
sshot-6.png
»bp

Author

Commented:
I am so sorry, I forgot to mention one important thing. The script is located in a wsf file. I tried your code, it works fine in a .vbs, and when placed it to a wsf it still allows me to put a breakpoint only to the first line.
<?xml version="1.0"?>
<job>
  <script language="VBScript"><![CDATA[

arrData = Array("1", "2", "3")
for i = lbound(arrData) to ubound(arrData)
    wscript.echo arrData(i)
next

' ]]></script>
</job>

Open in new window

Test your restores, not your backups...
Top Expert 2016
Commented:
Ah, yes, that makes a difference.  It sort of appears like it's perhaps treating the vbscript code as jscript, and that is causing the syntax errors.  I tried a few things and did a little searching but didn't find a useful vbscript in wsf example of VS debugging.

I did read though that VS2019 improves things in vbscript debugging, you might give that a try if you aren't working in 2019 currently.  I'm still 2017 and see what you are seeing for the WSF file.


»bp

Author

Commented:
Thank you for the suggestion, I'll give a try 2019 when I have some spare time.
Most Valuable Expert 2012
Top Expert 2014

Commented:
For the purpose of debugging, copy out the WSF code between the script tags and debug it separately as a VBS file.  Once you finish debugging, paste it back into the WSF wrapper. There shouldn't be any errors from the wrapper itself.

Regards,

Rob.

Author

Commented:
Rob,
WSF is used to include some additional libraries. That would be not quite effective to copy the libraries code into the main code when it need to be traced.
Most Valuable Expert 2012
Top Expert 2014

Commented:
Hmmm, OK.  Maybe you can use the old-fashioned WScript.Echo method and run the script from a command prompt using
cscript C:\YourScript.wsf

Place wscript.echo statements throughout the code where you are having trouble to see what is contained in variables.  You can echo Err.Number if you cannot show variables.

I would also recommend not using On Error Resume Next during debugging, and handling the errors properly.

It's a slower, and sometime painful, process I know, but it should work out.

I haven't tried VBSEdit for a long time, but it could debug WSF files better maybe: https://www.vbsedit.com/

Regards,

Rob.

Author

Commented:
Rob,
What you are suggesting is a way much more inconvenient than the issue I am experiencing. I am able to trace debug the script in VS, I just can't stop it wherever I want.
Yes, I am aware of "on error resume next" thing.

BTW, I have installed VS2019 and it is not able to debug a wsf at all (debugging a vbs is just fine). It says "Unable to open script document".
Most Valuable Expert 2012
Top Expert 2014

Commented:
Fair enough.  Without appropriate tools then, you may be a bit stuck. Give VBSEdit a go.  It has an annoying timer for the free version, but it may work better.

Rob.

Author

Commented:
Bill, I have installed VS2019 and also the "Windows Communication Foundation" component, and now the debug allows to put a breakpoint to any line of the script.
Thank you very much!
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Great, so glad that worked out, good follow up.

Bill