Strings as Variables
Microsoft® Windows® 2000 Scripting Guide
Strings are often used to assign values to variables. For example, the sample script in this chapter uses the following code to bind to WMI. (For information about binding to WMI, see "WMI Scripting Primer" in this book.)
Set objWMIService = GetObject("winmgmts:")
This code always connects you to the local computer. This is fine unless you are a system administrator responsible for managing a remote computer or two. In that case, you might want a script that can retrieve the free disk space from a remote computer. That would allow you to sit at your workstation and check the available disk space on any of the computers under your control.
When you are using WMI, it is possible to connect to a remote computer simply by including the computer name as part of the moniker passed to GetObject. For example, the following line of code binds to the WMI service on the remote computer atl-dc-01:
You can use the preceding code to write a script that binds to this one remote computer. In an enterprise setting, however, you might want a more flexible script, one that can bind to any remote computer.
One way to do this is to edit the script every time you run it, replacing one hard-coded computer name with another. A much better approach is to provide a way for the script to accept input as it runs, and thus operate against a computer whose name has been entered as a command-line argument.
User input methods will be discussed later in this chapter. Before that discussion takes place, however, it is important to understand how string values (such as computer names) can be assigned to a variable, and then used as part of the script code.
For example, in line 2 of Listing 2.6, the string value "atl-dc-01" is assigned to the variable Computer. In line 3, that variable is used to bind to the WMI service on the computer atl-dc-01. However, this is not done by hard-coding the value atl-dc-01 into the code, but instead by using the value of the variable Computer.
Listing 2.6 Using Strings
1 2 3 4 5 6
Const CONVERSION_FACTOR = 1048576 Computer = "atl-dc-01" Set objWMIService = GetObject("winmgmts://" & Computer) Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='c:'") FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR Wscript.Echo Int(FreeMegaBytes)
In a small demonstration script such as this, assigning the string to a variable actually requires more effort than hard-coding the value. However, this script does illustrate an important concept: You can assign a value to a variable, and then use that variable in place of a hard-coded value.
Why is that important? Imagine that this script was designed to retrieve free disk space from 100 computers. Instead of hard-coding separate WMI binding strings for each computer, you could create a single binding string using the variable Computer. Your script could then run that single line of code 100 times, each time replacing the value of Computer with a different computer name.
For the moment, however, you have to focus only on line 3 of Listing 2.6:
Set objWMIService = GetObject("winmgmts://" & Computer)
Here is how VBScript interprets that line of code:
It reads everything up to the second quotation mark. In other words:
Set objWMIService = GetObject("winmgmts://"
It reads the ampersand (&), which essentially means, "Append whatever comes next to the string." What follows the ampersand is the variable Computer, which has been assigned the value atl-dc-01. VBScript will now see the line as being:
Set objWMIService = GetObject("winmgmts://atl-dc-01"
It reads the closing parenthesis character. VBScript requires you to have an equal number of opening and closing parentheses. If the closing parenthesis is not included, you will receive an error message. VBScript now reads the line of code as:
Having reached the end of the line, it runs the statement. In turn, the script will connect to the WMI service on atl-dc-01. To connect to the WMI service on a different computer, all you have to do is change the value of the variable Computer.