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
CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

Command Prompt Tricks

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

Where is Microsoft NetMeeting in Windows XP?

...


Task Automation Whitepapers Essential Guide to E-discovery and Recovery for Microsoft Exchange

Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

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!

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.


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