Procedures

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

The first few system administration scripts you write are likely to be simple scripts that carry out a single task and then stop. For example, you might have a script that connects to a computer, retrieves a list of the services running on that computer, and then ends. You might have a second script that connects to a computer and starts a selected set of services, and a third that connects to a computer and stops a selected set of services.

As you become more proficient with scripting, you might begin to write more complex scripts, perhaps by combining several simple scripts. Instead of having three separate scripts for managing services, you might create a single service management script that can perform three different tasks depending on the command-line parameters entered. For example:

  • If you include /info on the command line, the script retrieves service information.
  • If you include /start on the command line, the script starts a selected set of services.
  • If you include /stop on the command line, the script stops a selected set of services.

As your scripts become more complex, you might find it beneficial to place portions of the code within a procedure. Procedures are lines of code that carry out a specific task. For example, your service management script might have three procedures: one to retrieve service information, one to start selected services, and one to stop selected services. Placing code within procedures makes it easy to identify and to isolate the tasks carried out by the script. This enhances the readability and maintainability of the script and also makes it easy to copy and paste the code into scripts that carry out similar activities.

Procedures are also useful for scripts that need to carry out the same task over and over. For example, you might want to echo a message each time an error occurs within a script. To do this, you can include the message display code at every point in the script where an error might occur. Of course, this requires extra work, not only to write the original code but to maintain it as well; if you ever decide to change the displayed message, you will have to locate and change each instance within the code.

A better approach is to create a procedure that displays the message and then call that procedure each time an error occurs. As a result, you have to write and maintain only a single instance of the display code.

VBScript allows you to create two types of procedures:

  • Subroutines. Code that is run but typically does not return a value. For example, you might create a subroutine to display error messages.
  • Functions. Code that is run and returns a value. Functions are often used to carry out mathematical equations. For example, you might pass free disk space to a function, and the function will, in turn, convert the free space from bytes to gigabytes and then return that value.

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