Click here to Skip to main content
15,883,955 members
Articles / Programming Languages / Javascript

Scripting on Windows

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Apr 2013Apache3 min read 8.5K   3   1
Scripting on Windows

Introduction

I needed to create a script of medium complexity that goes along these lines:

  1. Take a list of databases.
  2. Each database has an SVN folder, list of files to execute (with possible wildcards), and list of files to skip.
  3. For each database, perform “get” on the SVN folder, ignore the excluded files and run files from the “execute” list in the order specified, respecting the wildcards.

What do they use to write such scripts these days? My first attempt was in NAnt. We already use NAnt for continuous integration and it seems to work reasonably well with files and external programs. The problem I had with NAnt is that it lacks adequate control structures and data types. All properties are strings, and I needed structured data. The only type of for loop is iteration over a delimited string (or files in a directory). There are no parameterized function calls, although they can be simulated (poorly) with setting global properties and calling a target.

Then I briefly thought about writing it in...

C#

...but rejected the idea, since I wanted the script to be easily modifiable, and the compilation step gets in the way of that. Also, in compiled languages like C# script parameters are traditionally passed either via command line or via config file that must be parsed. My parameters were too big for the command line, and writing a parser was not in my plans. It is possible to do dynamic compilation in C#, but it is quite cumbersome. You don’t want your config file to start with something like using System; class Config { ConfigItem[] Data = ... , do you?

My next thought was...

Powershell

...but this one is shipped only with the newest version of Windows Server, and we are not there yet. On older versions, it requires installation with admin rights. It may be easier to get an audience with the Pope of Rome than to install an app with admin rights on a production box in a huge financial corporation.

I then settled on...

JavaScript (WSH) 

...and it worked, but I was plagued by various issues. First off, WSH does not seem to have a built-in way to execute an external command that sends output to current console window and wait for the result. You get either asynchronous execution without wait in current window (Exec), or optionally synchronous execution in external window (Run). I ended up writing a piece of code that uses Exec and then polls the process for the exit status.

Also, WSH version of JavaScript lacks support for include files. My script became big enough that I wanted it, primarily to separate config from code. One can use an eval call to read and execute external file, or use WSF files, but both of these options greatly mess up line numbers and make detecting errors virtually impossible.

JavaScript
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");

function include(fileName)
{
    eval(fileSystem.OpenTextFile(fileName, 1).ReadAll());
}

There are, of course, other alternatives: maybe I will try Python next time. But the situation with a scripting solution available on all currently active versions of Windows is bleak. Powershell would be a reasonably good answer if not for Microsoft’s love of admin-rights installers. Requiring an admin right installer is a huge demotivator in the corporate world.

This article was originally posted at http://www.ikriv.com/blog?p=1228

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions

 
QuestionInclude for WSH Pin
jsc4223-Apr-13 0:21
professionaljsc4223-Apr-13 0:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.