Thursday, June 19, 2008

Debugging VBScripts with Visual Studio .NET

How to debug a .vbs or .js file with Visual Studio .Net or Microsoft Script Debugger.

First off, your going to need a debugger... I prefer Visual Studio .NET but you can also use the MS Script Debugger.

First, you'll need a script to debug, so stick this code in notepad & save it to "c:\foo.vbs". Unless you already have your own of course.

'c:\foo.vbs
WScript.Echo "To Debut: Run -> cscript //d c:\foo.vbs"
WScript.Echo

num1 = 5
num2 = 13

'Here's your break point which will send this to the debugger.
STOP

WScript.Echo "num1: " & num1
WScript.Echo "num2: " & num2
WScript.Echo "num1 + num2: " & num1 + num2

WScript.Echo
Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
WScript.Echo comp & "\" & user[/code]


Now you can start debugging the .vbs or.js file from Start -> Run or the Command Prompt.

Type:
cscript //d c:\foo.vbs (To use STOP as a breakpoint)
cscript //x c:\foo.vbs (To starting debugging immediately)

As soon as the Windows Script Host hits the STOP that is located after the first few lines, Windows will prompt you to select a debugger. Alternately, you can stick a //x to start the debugger at the start.

I just want to say how happy I was when I found this because manually debugging scripts was a really big PITA. If you have any problems, check out Microsoft's KB article #308364 because it just may help...


Thanks for reading and please leave a comment to let everyone know how this helped you out.

2 comments:

Laurent Bugnion said...

Hi,

Your blog has a couple of interesting posts. However, you should really stop calling Visual Studio ".NET". .NET is the framework on which your application runs. Visual Studio is the tool (the IDE) you used to make it, and to debug it. You really confused me when you said you were using .NET to debug your application.

Greetings,
Laurent

pike said...

Thank you very much for you response. You are completely right. I have fixed my error and will try my hardest to remember for future posts.