Introduction to Calling Methods

Microsoft® Windows® 2000 Scripting Guide

Automation objects allow you to use their capabilities within your scripts. This enables you to create more powerful and more useful scripts than you could create if you were restricted to the functionality of the scripting language. For example, it is impossible to draw a chart or graph by using VBScript alone. Through Automation, however, you can borrow the capabilities of Microsoft Excel and easily add a chart or graph to, say, a Web page.

Automation objects typically expose both methods and properties. (However, there is no requirement for them to expose either.) Methods are equivalent to the actions that the object can perform. For example, although the script in Listing 2.2 is only three lines long, it uses Automation to access the methods of two different COM objects and thus performs two different actions. These two methods are:

  • The Get method, available through the WMI SWbemServices object. This method retrieves information for the specified object.

  • The Echo method, available through the WSH WScript object. This method displays information on the screen. If a script is running in a command window and thus under CScript.exe, this information is displayed within that command window. If the script is running under Wscript.exe, the information is displayed in a message box.

After you have created a reference to an object, you can call the methods of that object using dot notation. Dot notation is so named because you call a method by typing the name of the variable that references the object, a period (or dot), and the name of the method. (Depending on the method, you might also type method parameters.) Generally, dot notation uses the following syntax:


For example, in the following line of code, the SWbemServices Get method call is written in dot notation.

Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='c:'")

The parts of the SWbemServices Get method call are shown in Table 2.1.

Table 2.1 Parts of the SWbemServices Get Method Call




Object reference.


Dot (separates the object reference and the name of the method).


Method name.


Method parameter. For the Get method, this can be read as, "Get the instance of the Win32_LogicalDisk class where the DeviceID is equal to C:."


  • Instead of using Wscript.Echo to display the amount of free disk space, you can use the VBScript function Msgbox:

  • Msgbox objLogicalDisk.FreeSpace

  • In this book, however, Wscript.Echo is used instead of Msgbox. This is because the Msgbox function always displays its information in a graphical dialog box. When this dialog box appears, the OK button must be clicked before the script can proceed. For system administration scripts that display a large amount of data, this would be extremely tedious; it would also prevent the script from running automatically. By contrast, Wscript.Echo displays information as lines within a command window, provided the script is running under CScript.