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 <component> element. encloses the entire definition of a script component. All of the elements that follow must be between the <component> and </ component> tags.
- The <?component?> 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 <registration> 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 <registration> element to update the registry.
- The <public> element specifies the component's interface (e.g., the properties and methods it makes available). The <property> and <method> elements are both enclosed within the <public> element.
- The <property="propertyname"> 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 <property> element can also use the <put/> and <get/> 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 <property> element should include only a <put/> 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 <get/> 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 <put/> or <get/> element.
- The <method="methodname"> element declares a method for the component. The method name should match the name of a function in the script code. (As with the <property> element, if you want to use a different function name, use the internalname attribute.) If needed, use one or more <parameter="parametername"/> elements to declare parameters for the function.
- The <script language= "language"> element encloses the script code that implements the component. The language attribute tells the WSC runtime which language engine it should use to process the script. To avoid problems with the XML parser when using the <?XML version="1.0"?> element at the top of the file, place the script code between the XML <![CDATA\[ and ]]> markers.
Listing 1 shows the sample script component Sample.wsc. It provides a single write-only property, Number, and one method, Multiply. The Number property is write-only because it has only a <put/> element. When setting this property, the script component runtime executes the put_ Number function, with the new property-value as the parameter to the function. The Multiply method multiplies the Number variable by the parameter passed to the function and returns the result.
Registering and Unregistering a Component
To make a component available to scripts or programs, you have to register it on the system to add the appropriate data to the registry. The easiest way to register a script component is to right-click the WSC file in Windows Explorer and choose Register. You can also type the following command at a command prompt:
regsvr32 /i:<wscfile>
%SystemRoot%\system32\scrobj.dll
To unregister a component (i.e., remove its registration data from the registry), right-click the WSC file in Windows Explorer and choose Unregister, or type the following command at the command prompt:
regsvr32 /u /n /i:<wscfile>
%SystemRoot%\system32\scrobj.dll
To silently register or unregister a script component file from the command line, add the /s option after the Regsvr32 command. If the WSC file's path or filename contains spaces, you must enclose it in double quotes. You must be a member of the Administrators group to register or unregister a script component.