Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


April 2002

Quick Scripting Tips


RSS
Subscribe to Windows IT Pro | See More VBScript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
Main Article    Scripting Windows Management

Let's review a few general tips for scripting in VBScript. Future columns will include more specific tips that apply to the topic I discuss in the column, but these general tips will always apply.

  • Keep the lines in your scripts short. Short lines are easier to read. VBScript lets you use underscores to break lines, and statements such as If ...Then ... Else let you list conditions instead of crowding everything on one line.
  • Comment liberally. You know now what the statement you typed means. But will you remember what it means 6 months from now? Will someone else reviewing your code understand it? Make sure by spelling out what the pieces of the script do.
  • Mix case in your code. The mixed-case oDiskSpace is easier to read than lowercase odiskspace or uppercase ODISKSPACE. VBScript is case insensitive for most purposes (one exception is when you're computing the ASCII character values for letters: Uppercase letters have ASCII values different from lowercase letters).
  • When using Windows Script Host (WSH), use the command-line environment. WSH has two execution environments: a command-line one and a graphical one (the default). In the command-line environment, output appears in a command window unless you specifically send it to a dialog box. In the graphical environment, all output goes to message boxes. You'll want to use the command-line environment most of the time. Some operations won't work in a graphical environment, and if your script produces several lines of output, each line will have its own message box that will halt the script's execution until you click OK. To execute scripts in the command-line environment, you have two options: Preface each command you run with cscript, for example
  • cscript getfree.vbs


    or make the command line the default execution environment by typing

    wscript //h:cscript //s
  • Name variables according to the kind of data they represent (e.g., give strings names that begin with the letter s and objects names that begin with o). This practice makes debugging code easier. Some expressions won't work or will work differently with data of an unexpected data type. And if you know what data type you intended to feed a script, unraveling type-mismatch errors is easier.
  • Explicitly define variables up front. Although you can implicitly define variables by just assigning them values in the body of the script, use an Option Explicit statement at the beginning of the script to announce that only explicitly declared variables are permissible, then declare with a Dim statement each variable you'll be using. Disallowing the use of undefined variables helps you catch bugs introduced by mistyping variable names.
  • Write scripts in a text editor. Don't write them in a word processor, then copy them to a text editor for saving. Scripts use a lot of quotation marks. If your word processor converts straight quotes to smart quotes, when you copy the script to the text editor, junk characters will replace the quotes and the script won't work.

End of Article



Reader Comments
I am new to scripting-and espicially have been banging my head against the wall to perform admin functions using VBS. Can you help me with a simple example?

I want to learn how to write a scipt that will read a text or xls file and based ont eh list will remove or add computers. Yes I have several example scripts, but they are not easily understandable, and difficult to learn from.

Can you give me a very simple code-No Gui dialouge boxes, Just a step by step on how you read the file, attach to AD and loop. I know the basics, bu tI need a simple script that is explained step by step.

Thank you.

Samir November 05, 2003


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
No Jobs, No Excitement at Apple's Last Macworld Keynote

Apple CEO Steve Jobs made the right move in skipping out on his company's last appearance at Macworld: In a Tuesday keynote address at the conference, Apple had no interesting new products to sell, opting instead to spend mind-numbing amounts of time on ...

Where is Microsoft NetMeeting in Windows XP?

...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing