Script Tips

With a Trace

Add a trace-like window to debug scripts.

One key to successful, efficient debugging is getting insight into your script's operations. When variables contain values you didn't expect, or when object properties aren't set to what you thought they'd be, the result is often bugs. Script editors (such as VBSEdit, AdminScriptEditor, and OnScript) that instantiate the Microsoft Windows Script Debugger can provide some run-time insight into variables' contents; the new debugger in PrimalScript 4 Professional actually lists all your variables and their values.

But what if you don't have a debugger? A quick workaround is to build a "trace" window, using Internet Explorer. Just add this subroutine to the script you're debugging:

Dim oIE
Sub Trace(sMsg)
If Not IsObject(oIE) Then
Set oIE = CreateObject("InternetExplorer.Application")
  IE.navigate "about:blank"
  IE.ToolBar = False
  IE.AddressBar = False
  IE.Top = 10
  IE.left = 10
  IE.Width = 300
  IE.Height = 150
  IE.Visible = True
  IE.menubar = False
  IE.StatusBar = False
  IE.Document.Body.Title = "Debug Messages"
End If
  IE.Document.writeln Date & " - " & smsg & "
"
End Sub

Then, liberally sprinkle your script with calls to the Trace() subroutine. For example, anytime your script enters or exits a construct (such as If…End If, For…Next, or Do…Loop), add something like Trace("Now entering whatever"). Whenever a variable or object property is changed, add Trace("Variable xxx is now " & variable). Before calling object methods, add a Trace() statement indicating that you're doing it.

You'll be able to see your script execute in real-time, and the trace window will stick around after the script finishes so that you can examine it. Best of all, when you're finished debugging, just add Exit Sub as the first line of code within the Trace() subroutine, and the tracing stops!

Getting better insight into your script is a key debugging technique, and this subroutine (excerpted from my VBScript Debugging video) can be a big help.

About the Author

Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is an Author/Evangelist for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.

comments powered by Disqus
Most   Popular

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.