Calling Methods

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

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:

ObjectReference.MethodName

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

ItemDescription
ObjWMIServiceObject reference.
.Dot (separates the object reference and the name of the method).
GetMethod name.
("Win32_LogicalDisk.DeviceID= c: ")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:."

Note

  • 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.

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net