Previous Section Table of Contents Next Section

Validating User Input

This example logon script doesn't have any user input, but some of your scripts may. For example, you might write a script that asks for a server name, and then does some operation on that server. Any time you're asking for user input, you need to validate that input to make sure it's within the range that you expected.

For example, suppose you have a script that shuts down a remote server. You might have the script ask for the server name, and then ask for a shutdown delay in seconds. After accepting that input from the script's user (who might even be you), the script should check to make sure the server name was valid (perhaps it must start with two backslashes), and that the delay was within an acceptable range (maybe 5-30 seconds).

You can generally use If…Then constructs to validate user input. Why bother? Validation ensures that your scripts are working with proper input, and can help prevent the scripts from running into errors or performing unexpected actions.

Chapter 10 introduces If…Then, under "Conditional Execution."

If users provide incorrect or unexpected input, your script can display an error message and end, or even give users another chance to enter the necessary information.

TIP

Plan to add user validation to all scripts that accept input from a user. The examples in this book don't always include input validation; I've deliberately left it out in many cases to help focus on what the script is supposed to accomplish. Scripts used in the real world, however, should always validate user input.


    Previous Section Table of Contents Next Section