Downloads
38405.zip

I recently downloaded Microsoft's Scriptomatic utility from http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/wmimatic.asp. Scriptomatic generates Windows Management Instrumentation (WMI) scripts, so it's ideal for WMI scripting newbies like me. However, the utility generates scripts in VBScript only. How can I port a Scriptomatic-generated script to Perl?

Scriptomatic has no built-in capability for porting its VBScript code to Perl. However, with a bit of Perl and VBScript knowledge, you can port the code yourself. Porting a Scriptomatic script to another scripting language is simple. After you know how to port one Scriptomatic script, you can easily port any Scriptomatic script because the scripts all follow the same basic template.

For example, Listing 1 shows the VBScript code that Scriptomatic generates when you select the Win32_Environment class, which lets you manage environment variables on any WMI-enabled computer. I selected Win32_Environment as an example for two reasons. First, the class is relatively small: It defines only eight properties. Second, all WMI-enabled computers support this class.

Listing 2 shows the same script ported to Perl. Although spotting the changes is relatively easy, let's look at the more interesting changes. The most important change is in the first line, which imports the Win32::OLE module. Win32::OLE is the Perl module that provides Perl with the pixie dust it needs to work with COM automation libraries, such as the WMI Scripting Library.

The code at callout A in Listing 2 uses the Win32::OLE module's GetObject method to establish the WMI connection. Notice that Perl's dereference operator is an arrow (->) rather than a dot (.). Also notice that the string passed to GetObject isn't pieced together like the VBScript string. Perl supports interpolated strings, so you don't need to concatenate the pieces as you do in VBScript.

The last change worth noting is how you use Perl and Win32::OLE to reference and dereference COM collections. You use a scalar to reference the collection returned from the WMI Scripting Library's ExecQuery method. You use Perl's hash syntax to dereference an item in the collection (e.g., $ReferenceName->\{PropertyName\}). To learn more about using the Win32::OLE module to access COM automation libraries and components, see ActiveState's ActivePerl documentation at http://aspn.activestate.com/ASPN/Reference/Products/ActivePerl-5.6/site/lib/Win32/OLE.html.