In my last article I began showing you alternative languages that are compatible with WSH. In this installment I’m going to profile a few more alternatives and show you how to simplify code execution when running scripts in the Windows Script Host environment.
Next to Python, Perl is probably the most widely used WSH alternative. When Larry Wall first introduced Perl in 1987, I doubt he had any idea how quickly it would grow or the impact that it would have on the programming community.
Perl was originally designed for text manipulation but has become quite a favorite for system administration and web development. Perl remains a favorite of web developers and is probably the most common language used in CGI programs. Since Perl’s conception, preprocessing languages such as ASP and PHP have emerged, but many web developers still hold on to their roots.
Perl
Its widespread use and availability has made Perl a popular alternative for WSH as well. The language, while not always pretty to read, presents a fine mix of both power and efficiency. While Perl is used mostly by the Linux community, free Windows distributions such as ActiveState’s ActivePerl have helped to bring its popularity to a new platform. ActiveState’s installer also adds support for the WSH environment.
cscript //E:Perlscript myscript.pl
While WSH support is added by the installer, you will need to specify the scripting engine to use when running your scripts in the WSH environment using the command line above. You may wish to simplify this process by using the tip found at the end of this article.
Using Perlscript is pretty straightforward. You’ll need to use the WScript object’s CreateObject method to connect to OLE and ActiveX objects. Notice the -> syntax instead of the object dot syntax you’re probably used to seeing.
$files = $fso->GetFolder("C: emp")->Files;
Here again you can see Perl’s syntax as we use the FileSystemObject to return a collection of File objects for a specified folder.
Working with Collections
Perl provides two different ways of working with collections. I’ll demonstrate both of them for you.
foreach $file (in $files) {
print $file->{Name}, "n";
}
The simplest of the two methods uses a foreach structure. This method allows Perl to access the collection objects directly, as in VBScript. This example iterates through the collection and prints the name of each file back to the screen.
$object->{property}
Notice the special syntax that is used to return a property value. Because of this syntax difference, you must take special care to read the documentation for whatever object you use so that you can properly differentiate between its methods and properties.
$object->{property} = newvalue;
The same syntax structure is used to assign a new value to a property as well.
$enum = Win32::OLE::Enum->new($files);
The second method of working with a collection makes use of an enumerator object as in Jscript.
while (defined($file = $enum->Next)) {
print $file->{Name}, "n";
}
A While loop can then be used to iterate through the collection. Unless you have a specific need to use an enumerator, you’re probably better off sticking to the first, and simpler method.
Additional Notes About Perl
Active Perl creates two file associations for Perl. The .pl file extension is associated with perl.exe and the .pls (Perlscript) file extension is associated with WSH.
You should also avoid using the print command when running scripts under Wscript.exe. The print command writes to the StdOut stream, which is unavailable. You should use the WScript object’s Echo method instead.
You will also not be able to use familiar command line switches in WSH. You will need to set these option values in the script as demonstrated in the solution below.
By default, whenever you misspell or make reference to an object’s property or method that does not exist, Perl will not generate an error. This can make debugging quite cumbersome.
$^W = 1;
As author Brian Knittel points out in his book Windows XP Under the Hood, you can overcome this pitfall by including the line above at the beginning of your script.
Be sure to check out the link above to read my review of the book and read its profile on Amazon.com!
Finally, any command line arguments you provide when launching your Perl script in WSH will not be placed into the ARGV array. You will need to fetch these from the WScript object’s Arguments collection instead.
Object Rexx is an object-oriented programming language that was originally developed by IBM for its OS/2 and OS/390 line of operating systems. In late 2004, IBM released it as open source, giving rise to the free Open Object Rexx project.
Object Rexx, in its original form from IBM, is not offered for free. There is a fee to download the development environment; however, the client runtime version can be downloaded and installed for free.
This version of Open Object Rexx offers WSH support as well as compatibility for classic Rexx and other Open Object Rexx interpreters. Because Rexx is still quite widely used on IBM servers, it presents the next alternative for use in the Windows Script Host environment.
The Open Object Rexx installer found in the link above will create an .rxs file association for use with WSH. You will not need to supply an engine on the command line.
IBM’s Object Rexx development environment provides a very powerful debugger that is designed to minimize development time. Rexx’s efficiency and sophistication make it extremely popular among some system and network administrators.
The most common method of connecting to a COM object uses the OLEObject class. Notice the ~ syntax instead of the object dot syntax you’re probably more accustomed to seeing. Object Rexx also provides a GETOBJECT variation of this statement.
files = fso~GetFolder("C:temp")~Files
Working with Collections
Once again you can see Object Rexx’s unique object syntax as we use the FileSystemObject’s Folder object to return a collection of File objects.
do file over files
say file~name
end
Finally, Object Rexx is able to manipulate collection objects directly so we can create a loop to iterate over each object and print its name.
::requires "orexxole.cls"
In order to use ooRexx to access COM objects, you will need to include the above required line at the end of your script.
KixStart is a free-format scripting language developed by Ruud van Velsen of Microsoft Netherlands. It is primarily used for writing network logon and logoff scripts, but has been highly extended to allow use for complete system administration scripting. There is also a very good GUI-driven development environment available for KixStart. Since WSH is so often used for logon and logoff scripts, I’ve decided to profile KixStart as my next WSH alternative.
ScriptLogic has put together a very comprehensive set of documentation and samples for KixStart.
With such thoroughly written documentation available, I’m going to keep my explanation of KixStart very brief, but I will give you a working example of the sample script that we’ve been using all along.
$fso = CreateObject("Scripting.FileSystemObject")
As you can see, the CreateObject method is used to connect to the FileSystemObject.
$folder = $fso.GetFolder("C:temp")
$files = $folder.Files
You then use common object dot syntax to access properties and methods.
For each $file in $folder
? $file.name
next
Finally, KixStart provides a For each structure for iterating through the elements of a collection.
Ruby, created in Japan, is quickly emerging as a leader in the web development community. Its scriptable counterpart is ActiveScriptRuby, sometimes called RubyScript. Its functionality is similar to Java and its syntax is as simple as VBScript. These two qualities have helped to increase Ruby's popularity. While it has the power to operate as a system scripting solution, due to its web-dev nature, I highly recommend it for use in HTML applications.
Ruby boasts simplicity and ease-of-use. It uses a very natural syntax that makes it very easy to learn. Ruby is also very well documented. Keep in mind that most of this documentation was originally written in Japanese and painstakingly translated to English. Better written English documentation is emerging as Ruby is quickly gaining popularity in Europe and the Americas.
require 'win32ole'
Begin your Ruby script with this line if you want to connect to standard COM objects.
fso = WIN32OLE.new('Scripting.FileSystemObject')
A connection can then be made using the Win32OLE class.
folder = fso.GetFolder('C:temp')
for file in folder do
puts file.name
end
Ruby then uses a common object dot syntax to access an object’s properties and methods along with a simple for structure for iterating through the elements of a collection.
In these last two articles, I’ve profiled what I would consider to be the most likely alternative languages for use in the Windows Script Host environment. This list is by no means comprehensive. There are dozens more WSH-enabled languages available for you to choose from. A Google search and a little research may reveal a version of your own favorite that is also WSH-enabled. Here’s a short list of some additional languages that I know of that offer WSH support.
As you’ve seen throughout my samples, many of these languages require you to specify an interpreter engine on the command line in order to run your scripts in the WSH environment. The tip I’m about to show you will allow you to double-click a script file or run it directly from the command line as you can with .vbs and .js files now.
You will have to modify the registry. As with any other registry changes, you should first create a backup in case something goes wrong.
I’m going to create a .pys (PythonScript) file extension. To begin, you’ll need to open Notepad and copy the text below. You may also download my file here.
The first point of interest is the file extension of the first line (.pys). This should be changed to reflect the file extension that you wish to use.
The second point is the pysfile name. This can be changed to whatever you like. It should typically reflect your file extension. Be sure to change every instance. It appears in nearly every line! (Try using the Replace… command on the Edit menu).
Now change the value listed under ScriptEngine to reflect the proper script engine for your file type.
Finally, save your file with a .reg extension. Now when you double-click it, your file should add the appropriate changes to the registry. Your file type is now associated with the Windows Script Host!
The Windows Script Host provides a very flexible scripting environment. As you can see, it supports much more than just the VBScript that you are used to seeing. Don’t be afraid to take advantage of Python’s superb string handling or Perl’s regular expressions. Whatever your need or want, you now have the ability to use WSH in nearly any language you might choose. Until next time, keep coding!