Downloads
49092.zip

A Windows Script Component (WSC), formerly known as a server scriptlet, is a COM component (also known as an ActiveX object or an OLE automation object) implemented in a script language. A WSC file is an XML-format text file that uses the .wsc file extension. One of the main advantages of WSCs is code reuse: The object implemented by the WSC is available for use in any Windows Script Host (WSH) script. Let's look at the elements of WSC files and a real-life example of how to use them.

Understanding COM Components
To understand the WSC file format, it helps to have a basic understanding of COM components. COM is one of the core technologies that make WSH so useful. A WSH script can use a host of COM components to access functionality that the script language itself doesn't provide. For example, VBScript doesn't by itself provide any way to access the file system, but you can use the Scripting.FileSystemObject COM component in a VBScript script to open files. To get real administrative work done, WSH scripts rely heavily on COM components.

You can create your own COM components entirely in script code. Coding your own components provides a convenient way to create standalone objects that can be freely used by multiple scripts—or even by other programming languages that support COM components, such as Visual Basic (VB).

Understanding WSC Files
As I mentioned earlier, a WSC file is an XML-format text file that provides a set of elements and attributes that define an object's interface (e.g., the properties and methods available to calling scripts) as well as its implementation in a script language. For those who aren't familiar with XML, I provide a short list of its syntax rules in "WSH, Part 2: .wsf Files," February 2006, InstantDoc ID 48692.

WSC files have their own list of XML elements:

  • The element. encloses the entire definition of a script component. All of the elements that follow must be between the and tags.
  • The element allows for more extensive error checking in the component. It uses two attributes: error and debug. Use error="true" to display more detailed error messages, and use debug="true" to be able to step through the component's code in the script debugger.
  • The element provides the information needed to register the component as a COM component on the system. It has four required attributes, as shown in Table 1. You can generate a valid globally unique identifier (GUID) by using the Windows Script Component Wizard (see the Additional Resources box) or the guidgen.exe tool in the platform software development kit (SDK). When you register or unregister the component (a process I'll describe shortly), the WSC runtime uses the information in the element to update the registry.
  • The element specifies the component's interface (e.g., the properties and methods it makes available). The and elements are both enclosed within the element.
  • The element declares a property for the component. For a simple read/write property, the < property> element references a global variable in the script code. The global variable's name should match the property's name unless you include the internalname attribute to reference a different variable. The element can also use the and elements (they don't have closing tags) when you want to call code when setting and/or retrieving the property's value or when you want to create a write-only or read-only property. To create a write-only property, the element should include only a element. The script code should define a function named put_propertyname, where propertyname is the name of the property. Conversely, a read-only property should include only a element, and the script should have a function called get_propertyname. You can use different property function names by adding the internalname attribute to the or element.
  • The element declares a method for the component. The method name should match the name of a function in the script code. (As with the element, if you want to use a different function name, use the internalname attribute.) If needed, use one or more elements to declare parameters for the function.
  • The