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 


September 2004

Scripting Best Practices


RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints
Or get the Monthly Online Pass—only $5.95 a month!
Main Article    Programmatically Protect Your Passwords

1. Put all the configuration information at the beginning of the script. That way, you're editing only one section in the script, minimizing the chance of introducing errors.

2. If your script will write information to the same output file several times, use a variable to represent the file's path instead of hard-coding the file's path each time. This practice not only reduces the chance of specifying an incorrect path but also saves time. If you need to change the file's path, changing the path once is much quicker than having to search through all the code and replace the path in each spot it appears.

3. Make life easy on yourself, your coworkers, and whoever follows in your footsteps by including comments in your scripts. The code you write might make a lot of sense today, but tomorrow it might look like an unknown language. On more than one occasion, I've tried to figure out who wrote a script, only to discover it was a script I created a few years earlier.

The common way to comment Windows shell scripting code is to put either Rem or double colons (::) at the beginning of each comment line. Unlike some other scripting languages, Windows shell scripting doesn't let you put comments on the same line as the code. Put your comments in the lines preceding the code.

4. Consistently use intuitive variable names. For example, if you're using a variable that contains a user ID, name it UserID rather than Var1. Using intuitive variable names will make it easier for you to read a script and easier for your coworkers or successor to understand it. Using intuitive variable names will also make it easier for you to reuse the code in other scripts.

5. Put your tools in a common folder that has no spaces in its path. As you begin to write scripts, you'll quickly go beyond using internal Windows commands to using Microsoft utilities (e.g., resource kit tools, support tools) and third-party utilities. Hunting down the location of a utility you used previously can be frustrating.

Putting all your tools in a shared folder is best because you can then point to a central location in all your scripts. However, tools sold as licensed software might have restrictions on their deployment to multiple nodes. If you have such a tool, you first need to confirm that you have an appropriate site or multinode license.

Currently, Windows Support Tools and Windows resource kits are installed, by default, into an installation path that contains spaces. For scripting purposes, a path with spaces isn't recommended. When paths with spaces are embedded inside the commonly used For command, the spaces can cause cryptic errors that are difficult to diagnose. Be diligent and change the installation paths for all your utilities so those paths are free of spaces.

6. Always carefully test your scripts in a nonproduction environment before you run them in your production environment. If a script that simply creates reports or performs data queries isn't working properly, not too much fallout can happen beyond the script producing inaccurate reports or data. However, if a script that deletes files or modifies settings isn't working properly, the fallout can be devastating. Although you can incorrectly delete files or modify settings with a system's built-in tools, a poorly written and improperly tested script can do a lot of damage much faster. By carefully and thoroughly testing scripts in a nonproduction environment, you'll minimize risk of unintended changes in your production environment.

End of Article



Reader Comments

You must log on before posting a comment.

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




Top Viewed ArticlesView all articles
VMware and the Future of Virtualization

What's next for virtualization and business IT? Windows IT Pro senior editor Jeff James speaks with VMware President and CEO Diane Greene on the future of virtualization technology. ...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

What service packs and fixes are available?

...


Task Automation Whitepapers Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

A Preliminary Look at Deployment Plans for Microsoft Windows Vista

Related Events Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation 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.


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Critical Challenges of ESI & Email Retention
Are you storing too much electronic information? Get expert legal advice and better understanding of what you are required to do as an IT professional.

Become a fan of Windows IT Pro on Facebook!
Join us on Facebook and be a fan of Windows IT Pro!

Sustainable Compliance: Are You Having a Resource Crisis?
Read this white paper to examine trends in compliance and security management and review approaches to reducing the cost and operational burden of compliance.

Rev Up Your IT Know-How with Our Recharged Magazine!
The improved Windows IT Pro provides trusted IT content with an enhanced new look and functionality! Get comprehensive coverage of industry topics, expert advice, and real-world solutions—PLUS access to over 10,000 articles online. Order today!

Get It All with Windows IT Pro VIP
Stock your IT toolbox with every solution ever printed in Windows IT Pro and SQL Server Magazine plus bonus Web-exclusive content on hot topics. Subscribe to receive the VIP CD and a subscription to your choice of Windows IT Pro or SQL Server Magazine!



Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.
Windows IT Pro Home Register About Us Affiliates / Licensing Media Kit Contact Us/Customer Service  
SQL Connected Home IT Library SuperSite FAQ Wininfo News
Europe Edition Office & SharePoint Pro Windows Dev Pro Windows Excavator 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing