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
The Memory-Optimization Hoax

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

The Desktop tab is missing from the Display Properties in Windows XP?

...

Microsoft's Olympic Gold

With world records being broken at a dizzying pace, the 2008 Summer Olympics in Beijing has drawn massive audiences from around the world, most watching the games via traditional TV coverage. But behind the scenes, a massive array of technology is ...


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 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.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

WinConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

Deploying SharePoint! In-Person Event Series – 8 Cities
Discover best practices and tips for deploying the perfect SharePoint infrastructure. Early Bird Price of $99 extended till Sept. 15th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



When managing just VMware isn’t enough
Plan/Manage/Secure – NetIQ VMware management. Download whitepaper.

What’s up with your network? Find out with ipMonitor
Availability monitoring for servers, applications and networks – FREE trial

Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

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.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16 in London.
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 Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 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