Arrays

Microsoft® Windows® 2000 Scripting Guide

Collections are an excellent way to package information because they allow you to work with any number of items, even if you do not know the details of any of those items. For example, the script introduced in Listing 2.8 allows you to retrieve the amount of free disk space for all the drives installed on a computer, even if you have no idea how many drives are installed on that computer. To carry out an action on each item in the collection, you simply use a For Each loop to iterate through the collection item by item.

Automation objects can create collections for you. However, you might have other information (information not returned from an Automation object) that might be easier to manipulate if you can iterate through the set of items one by one. Suppose you want to check the available disk space on three computers rather than just one. You can write the code to check the first computer, copy and paste the code, and then modify the pasted code to check free disk space on the second computer. You can repeat this process again to check for free disk space on the third computer.

Although this approach works, it can quickly become tedious, particularly if you need to check 100 computers. In addition, suppose you need to make a change in the code, perhaps to return not only the free space on the drive but also the total size of the drive. To make that change, you need to change all 100 instances of the code, a process that not only takes a long time to complete but greatly increases the likelihood that you will make an error somewhere along the way.

A better approach is to use a For Each loop to iterate through a collection of computers, checking for free disk space on each one. This can be done by placing the computer names in an array, a data structure that can be used in much the same way as a collection.

The script in Listing 2.12 places the names of three computers (atl-dc-01, atl-dc-02, and atl-dc-03) in an array and then uses a For Each loop to connect to and retrieve free disk space information from each computer. In line 3, the script creates an array named Computers by using the Array function and specifying the three computer names as the function parameters. (The names are enclosed in quotation marks because they are strings.) In line 4, a For Each loop is used to iterate through all the items in the Computers array.

Listing 2.12 Using an Array

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Const CONVERSION_FACTOR = 1048576
Const WARNING_THRESHOLD = 100
Computers = Array("atl-dc-01", "atl-dc-02", "atl-dc-03")
For Each Computer In Computers
 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 Computer & " " & objLogicalDisk.DeviceID & _
 " is low on disk space."
 End If
 Next
Next

Although arrays are similar to collections, there is one primary difference. As a script writer, you have little control over collections. If you query WMI for a list of disk drives, you will get those disk drives back in whichever order WMI chooses. Furthermore, you will not be able to readily access individual drives without iterating through the entire collection.

By contrast, you can control the order of information in an array because you typically populate the array yourself. Furthermore, you have the ability to access individual items in an array without having to iterate through the entire set. This is because each item in an array is assigned an index number. In VBScript, the first item in an array is assigned index number 0, with subsequent items assigned index numbers 1, 2, 3, and so forth. The array created in Listing 2.12 therefore contains the items and index numbers shown in Table 2.3. Notice that the highest index number will always be 1 less than the number of items in the array.

Table 2.3 Index Numbers in an Array

Index Number

Item

0

atl-dc-01

1

atl-dc-02

2

atl-dc-03

You can use these index numbers to access individual items within the array. For example, this line of code will echo atl-dc-02, the value of item 1 (based on index number) in the array:

Wscript.Echo Computers(1)

To echo the value of a different item in the array, simply replace the value 1 with the appropriate index number.

Additional methods for creating arrays and for accessing the individual items within those arrays are discussed later in this chapter.