Adding a Trace Routine to a Script

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Trace routines allow you to track a program as it runs and help you identify lines of code that produce unexpected results. You can add a rudimentary trace routine to your scripts by having the script periodically report its status.

For example, you might echo the value of your variables each time those variables are changed in some way. This enables you to identify any spot in the script where a variable is not assigned the correct value (a common cause of run-time errors). Or you might display a message each time you enter a procedure or function. If a message does not appear, you know that the function or procedure was not called.

The following script projects the number of support calls to a help desk by doing the following:

  • Requests the total number of calls for two weeks.

    Assume that there are 93 calls in the first week and 96 in the second week.

  • Adds the number of calls for the first two weeks together.

    The total of calls for the first two weeks is 189.

  • Multiplies the two-week total by 26, giving a projection of the total number of support calls for the next year (52 weeks).
  • Divides the total number of calls by 8 support technicians to determine the anticipated number of calls each technician will need to handle in the coming year.
intWeekOneCalls = InputBox("Enter the number of support calls for Week 1:") intWeekTwoCalls = InputBox("Enter the number of support calls for Week 2:") intTotalCalls = intWeekOneCalls + intWeekTwoCalls intProjectedNumberOfCalls = intTotalCalls * 26 sngCallsPerTechnician = intProjectedNumberOfCalls / 8 Wscript.Echo  sngCallsPerTechnician 

The script returns a value of 30,537 as the number of calls each technician can expect to handle. Unfortunately, the correct answer is 614.25.

To make matters worse, determining where the problem lies is difficult, because no error messages are returned, and both the scripting syntax and mathematical formulas appear correct.

One solution is to insert a trace routine that periodically reports the value of the variables used in the script. For example, the following revised script uses the Echo statement to display the value of a variable each time that value changes.

intWeekOneCalls = InputBox("Enter the number of support calls for Week 1:") Wscript.Echo intWeekOneCalls intWeekTwoCalls = InputBox("Enter the number of support calls for Week 2:") Wscript.Echo intWeekTwoCalls intTotalCalls = intWeekOneCalls + intWeekTwoCalls Wscript.Echo intTotalCalls intProjectedNumberOfCalls = intTotalCalls * 26 Wscript.Echo intProjectedNumberOfCalls sngCallsPerTechnician = intProjectedNumberOfCalls / 8 Wscript.Echo  sngCallsPerTechnician 

When this script runs, you will see that the sum of the week 1 and week 2 totals (93 + 96) does not equal 189. Instead, VBScript returns the value 9396. Why? B intWeekTwoCalls ecause it is treating the two variants as string variables rather than integers. As a result, the variables are concatenated (93 & 96 = 9396) and not added (93 + 96 = 189). You have now located the problem.

To solve this problem, you can use the data type features in VBScript to cast these two variables as integers, as shown in the following code snippet:

intWeekOneCalls = InputBox("Enter the number of support calls for Week 1:") intWeekTwoCalls = InputBox("Enter the number of support calls for Week 2:") intTotalCalls = Cint(intWeekOneCalls) + Cint(intWeekTwoCalls) Wscript.Echo intTotalCalls intProjectedNumberOfCalls = Cint(intTotalCalls) * 26 Wscript.Echo intProjectedNumberOfCalls sngCallsPerTechnician = Cint(intProjectedNumberOfCalls) / 8 Wscript.Echo  sngCallsPerTechnician 

Tip

  • You can also have your script periodically write trace information to a log file. This enables you to run the script without interruption and then later read the log file and determine whether the script ran as expected.

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net