Testing Multiple Conditions

Microsoft® Windows® 2000 Scripting Guide

The scripts used in the first half of this chapter made a decision based on a single condition: If the amount of free disk space was less than a specified amount, an alert was triggered:

If FreeMegaBytes < WARNING_THRESHOLD Then
    Wscript.Echo objLogicalDisk.DeviceID & " is low on disk space."
End If

There might be times, however, when you want to take action only if two or more conditions are met. For example, you might want immediate notification if your e-mail servers start running out of disk space. For other servers, you might not need immediate notification. Instead, you can simply check the log at some point to verify that these servers have adequate disk space.

In a situation such as this, you want an alert to be triggered only if two conditions are true: the amount of free disk space has dropped below the specified level and the server in question is an e-mail server. You can test for multiple conditions by using the logical operator And.

In the following script fragment, an alert is triggered only if free disk space has fallen below the threshold and the computer in question is an e-mail server. If either of these two conditions is not true (that is, either there is adequate disk space or the computer is not an e-mail server), no alert will be triggered.

If FreeMegaBytes < WARNING_THRESHOLD And ServerType = "Email" Then
    Wscript.Echo objLogicalDisk.DeviceID & " is low on disk space."
End If

On other occasions, you might want to take action if any one of a specified set of conditions is true. For example, you might want an alert if free disk space has fallen below a specified threshold or if available memory has fallen below a specified threshold. The decision matrix for this type of script is shown in Table 2.17.

Table 2.17 Decision Matrix

Adequate Disk Space?

Adequate Memory?

Trigger Alert?

Yes

Yes

No

Yes

No

Yes

No

Yes

Yes

No

No

Yes

To make this type of decision, use the logical Or operator, as shown in this code snippet:

If FreeMegaBytes < WARNING_THRESHOLD Or AvailableMemory < MEMORY_THRESHOLD Then
    Wscript.Echo "Computer is low on disk space or memory."
End If

The logical Or operator returns True if either condition is true (or if both conditions are true). In the preceding example, if FreeMegabytes is less than the warning threshold or if Available Memory is less than the memory threshold, the condition is true, and the warning message is echoed. Thus, the warning is displayed if the computer is running low on memory, even if free disk space is adequate.