Alintex Script.NET allows you to create scripts written in C#, VB.NET, JScript.NET and J#. This makes it a very powerful scripting engine because you can use the .NET Framework classes to perform almost any task imaginable.

It might not be as easy as VBScript or JScript because the .NET Framework classes can be daunting and confusing, especially for non-programmers.

How can you write a script in a .NET language? The basics are simple (as always), as the example below shows:

#region Script
print("Hello World!");
#end region

The script above is written in C# and can be put in a file called hello.csx. You can then execute the script by double clicking or by just executing it from the command line. If you installed the scripting engine, the csx (and vbx, etc...) will be registered to execute using axwscript.exe. The scripting engine (axscript or axwscript) will compile the script if it has changed and then run it.

The #region blocks and the print statement are specific to the scripting engine. print actually gets expanded to System.Console.WriteLine.

You can also omit the #region block and write the script like you would write a .NET console program.

using System.Windows.Forms;
using System;
using System.IO;
using System.Diagnostics;
class Script
{
 public static void Main(string[] args)
 {
}
}

The code above is like a .NET console program. The class needs to be called Script though, because that is what the scripting engine expects. You can use another class name if you wish. In that case, you need to execute the script with axscript.exe or axwscript.exe followed by a parameter that specifies the different class name.

Of course, if you write your scripts like a standard .NET console program, you might wonder what is the advantage of using this scripting engine because you could compile (using vbc.exe or csc.exe) the script and distribute the resulting exe. The answer is simple: compiling would make it less interesting for administrators because they might want to adjust the script a bit before running it on their system. If you do decide to compile the script, it is very simple: csc scriptname.csx (for C#, use vbc for VB.NET).

Distributing the scripts is easy: just distribute the source files and make sure that the system you are running the scripts on has the engine installed (and the .NET Framework of course). If the scripting engine is not installed on the target system, just distribute axscript.exe or axwscript.exe together with your script(s). Your script can then be run using: axscript scriptfile.csx.

Another advantage of using a .NET scripting engine is the ability to create a GUI in code to get input from the user. For instance, you could check if the user provided parameters on the command line and if not, launch a GUI to ask for them.

I have written a small script that just does that. It is a script to create a virtual machine from a template. I blogged about that earlier but that script was written as a simple batch file. The .NET script does the same thing and illustrates the following stuff:

  • Reading command line parameters.
  • Reading and writing to files.
  • Launching external programs.
  • Writing to the application event log
  • Creating and launching a GUI when the user does not specify a parameter.

I only scratched the surface here but the sample script can be used as a start for your own projects.

One more tip: to easily write scripts like this, I can recommend Notepad2. It is a really good editor and will color code your code as well. Forget about notepad.

DISCLAIMER: I am an administrator-type, not a programmer, so don't complain about the code.