Making Decisions

Microsoft® Windows® 2000 Scripting Guide

One of the primary reasons for using scripts as a system administration tool is that scripts reduce the need for hands-on human intervention. The scripts introduced thus far in this chapter go a long way towards this goal; the version shown in Listing 2.8, for example, can connect to any computer (even a remote one), bind to the WMI service, and determine the amount of free disk space on each of the hard drives installed on that computer. By running this script on a regular basis, administrators can receive advance notification if any drive begins to run low on disk space.

However, it is still up to a system administrator to analyze the script output and determine whether a disk is running low on disk space. The script can be improved by examining the amount of free space and issuing a notification only if the free space has dropped below a specified level. With this approach, administrators are notified only if a disk is running out of space. No notification means that all the disks are in compliance and no action is required.

VBScript provides a number of programming constructs that allow scripts to "make decisions." This means a script can analyze a particular piece of data and then take a specified course of action based on the value of that data.

The simplest form of decision-making code is the If Then statement, which examines the value of a particular piece of data and compares it against a predetermined value (for example, if the amount of free disk space is less than 100 megabytes). If the statement is True (for example, if only 99 megabytes of disk space are available), the script carries out some action. If the statement is not true, no action is taken.

This simple type of decision-making is shown in Listing 2.10. In line 8, the script checks to see whether the amount of free space is less than 100 megabytes (by comparing the value with the constant WARNING_THRESHOLD). If this conditional statement is True (for example, if a drive has only 99 megabytes of free space), the statement immediately following the If-Then statement runs. In this script, that statement appears on line 9, which echoes the message that the drive is low on disk space.

If the conditional statement is False (for example, if the drive has 101 megabytes of free disk space), line 9 is not run. Instead, processing passes to line 10, which marks the end of the If Then code block, and the script continues with line 11.

Listing 2.10 Making Decisions


Computer = "atl-dc-01"
Set objWMIService = GetObject("winmgmts://" & Computer)
Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk")
For Each objLogicalDisk In colLogicalDisk
 FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
 If FreeMegaBytes < WARNING_THRESHOLD Then
 Wscript.Echo objLogicalDisk.DeviceID & " is low on disk space."
 End If