Scripting can play a valuable, timesaving role in network administration. In my previous article, I discussed the benefits of using scripts and various ways they’re used. Now we will take a look at some of the choices you have in scripting languages and platforms.
You can think of a scripting platform as an environment in which a script can run. Given that a script is nothing more than a collection of text, there has to be some means for the computer on which the script is running to understand that text and carry out its instructions.
In Windows, there are two major scripting platforms to choose from: the Windows shell and the Windows Scripting Host (WSH). We’ll cover the Windows shell first and then look at the WSH, along with its two scripting languages, VBS and JScript.
A shell is nothing more than an interface that allows a user to communicate with, or issue commands directly to, the operating system. The concept of a shell has been around in UNIX for many years. In fact, there are several shells in the UNIX world, each with its own features and commands that make it suitable for various tasks.
In Windows, there is no such diversity. You have only one shell, the Windows shell, which is built into the operating system. And you are undoubtedly already familiar with the interface, although you probably call it the command prompt or, if you’re a real old-timer, perhaps the DOS prompt. Technically speaking, it’s called a command shell and is run by executing the file Cmd.exe, found in C:\Winnt\System32. Probably the easiest way to run it is to simply click Start | Run, type cmd in the text box, and click OK, or create a shortcut to Cmd.exe.
The Windows shell comes with a set of built-in commands, many of which are well known and commonly used, such as dir, copy, del, cd, etc. Commands and their associated parameters are usually issued one at a time at the command line. More important for our purposes is the fact that commands can also be used in a batch mode. That is, using a text editor, you can write a separate command on each line, saving the finished product with the extension of either .bat or .cmd. This turns the text file into an executable that will be run as an interpreted program, carrying out each command one line at a time, in order. This is what we call shell scripting.
Although the Windows scripting language is far from being a full-scale programming language, it does come with some useful commands and features that allow it to have some of the flexibility you’d expect to find in a program. Some of these features are:
You can have your script test to see whether a certain condition exists, and if it does, do one thing, and if it doesn’t, do something else.
Every time a command is carried out, Windows generates an error level, with error level 0 being “no error.” This allows you to include a provision in your script to gracefully exit from an error it might encounter.
Information about a given computer and the user who is logged on to that computer can be found in the registry, at HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER. Some of that information, which can be of use in scripting, is available in the form of system variables.
To get an idea of what is available, you can open the command shell and type the command set. This will display a list of all the system variables and their current values. These can then be referenced in a script by bracketing them with the percent symbol. For instance, %username% will refer to the username of whoever is currently logged on to the computer. An example of its use would be to copy the current user’s Favorites folder and all subfolders on the local machine to that user’s home folder on the server:
xcopy %userprofile%\favorites \\fileserver\home\%username% /s/y.
I will talk more about these features in a future article devoted to shell scripting basics. You’ll find a reference for all of the commands available for shell scripting in Windows Help. If you are using Windows 2000, click on Start | Help. In the Search tab, type command reference and click List Topics. Then, under Select Topic To Display, double-click on the topic Windows 2000 Command Reference Main Page.
In Windows NT, click on Start | Help. In the Find Tab, type command. Then, in the Pick A Topic pane, select Commands Index and click the Display button.
Windows Scripting Host
Although the shell scripting language remains a fixed part of the operating system, WSH can be separately updated and upgraded, since it exists as separate files. In addition, it can be installed on several versions of Windows. To download the latest version of WSH, go to the Microsoft Windows Script Page and follow the link to the download page. To determine which version is currently installed, type cscript at the command shell.
The WSH makes use of a rather strange concept called an object model, which can take some getting used to for a newcomer to scripting and programming. You can think of an object as a tool that you use to accomplish certain tasks. Each object has a set of methods associated with it. You can think of methods as the functions or capabilities of each tool, or object. The root object for WSH is called WScript, and from it, other objects can be created and used within scripts to accomplish tasks.
Both VBS and JScript are object-based languages, and each uses its own object model that works in conjunction with the WSH object model.
Visual Basic Scripting Edition
In future articles, I will discuss the basics of using VBS for writing scripts. For the moment, let’s take a quick look at what it can do.
VBS is a subset of the full-blown programming language Visual Basic. (Another subset of Visual Basic, called Visual Basic for Applications—VBA—is used primarily to create macros for Microsoft Office applications.) Using the objects and methods found in both the Windows Scripting Host and in VBS, along with correct VBS syntax, you can create a VBS script.
You do this by using a text editor such as Notepad to write out a set of commands and then save the finished script with the file extension .vbs. Once this file is created, you can execute it just as you would any other executable file. For example, you can:
- Double-click on the icon.
- Type the filename at the command prompt.
- Type the name of the file on its own line within a shell script.
You create a JScript file in the same way you create a VBS file, except that you must save it with the file extension .js. The default icon for a JScript file looks just like the icon for a VBS file, except that the VBS icon is blue and the JScript icon is yellow. You execute a JScript file in the same way you execute a VBS file.
Which one to use?
It is even possible to use both scripting languages in the same script, if you save it with the file extension .wsf (Windows Script File). However, learning both languages requires twice the time, so it’s probably best to stick to just one. For the purposes of this series of articles, I will concentrate on VBS rather than JScript simply because that is my preference.
Onward and upward
I’ve given a brief introduction to Windows shell scripting, the WSH, and the two built-in WSH scripting engines for VBS and JScript. In my next article, I will explain how to use Windows shell scripting for network administration. I will follow that up with articles about scripting with VBS for network administration, programming concepts you need to know in both platforms, and further resources for learning and improving your scripting skills.