Incrementally Running a Script

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Suppose you woke up this morning and discovered that your car would not start. How would you handle this situation? You could call a tow truck and have the car towed to the nearest mechanic. Or, if you are a do-it-yourselfer, you could immediately take the engine apart and begin looking for broken or worn-out items.

Of course, both those approaches might be a bit extreme; after all, what if the car is simply out of gas? Instead of taking the entire cart apart, you are likely to take a a step-by-step approach to trying to figure out what it wrong. For example, you might check whether there is gas in the gas tank or whether the battery is still charged. Most likely, your diagnosis and troubleshooting will involve incremental steps, identifying possible problems, and then investigating each one in turn.

This same technique can be used for debugging scripts. Rather than read through each line of code, trying to determine where the problem might be, you might instead run part of the script and then quit. This can help you determine the exact location where the problem occurs. For example, you might run half the script and then quit; if the script runs without errors, you can generally assume that the problem is in the second half of the script. You can then continue to run a selected portion of the script until you finally locate the problem.

To run only part of a script, insert a Wscript.Quit statement at the point where you want the script to stop running. For example, the following script snippet creates a new organizational unit and then adds a new user to it. To troubleshoot the script, first verify that the section that creates the organizational unit works as expected. To ensure that only this portion of the script is run, insert Wscript.Quit at the end of this section.

Set objRootDSE = GetObject("LDAP://RootDSE") objDomain=rootDSE.Get("DefaultNamingContext") strOUName = "Accounting" Set objContainer = GetObject("LDAP://" & objDomain) Set objOU = objContainer.Create("organizationalUnit", "OU=" & strOUName) objOU.SetInfo Wscript.Quit strUserAccountName = "rsmith" strSAMAccountName = "rsmith" set objUser = objOU.Create("user", "cn=" & strUserAccountName ) objUser.Put "samAccountName", strSAMAccountName objUser.SetInfo 

After creating the organizational unit, the script stops before creating any user accounts. If the organizational unit is not created, you can then focus your debugging efforts on that section of the script before moving on to the section that creates user accounts.

The Wscript.Quit method also allows you to check the syntax of your script without actually running that script. To check syntax without running the script, insert Wscript.Quit as the first line. When you run the script, the script host checks the syntax and reports any errors. If the script host discovers no syntax errors, it attempts to run the script. Because the first command is Wscript.Quit, it runs only that line and then stops.


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