Many people find the idea of administrative scripting intriguing, but also intimidating. This is especially the case if you're not sure what you can accomplish with a script that you can't accomplish from a GUI. You may fear that the learning curve will be excessive, and if you've decided that scripting is too arcane to bother with, you're not alone.
However, scripting is a lot simpler than it looks. If you begin at the beginning, you can save yourself substantial time and effort.
My new column will help you learn to script in small steps. In today's premiere column, I'll explain when to script a task, and I will introduce some basic concepts you'll find useful. Next month we'll explore some more advanced concepts: procedures and operations, and I will expand on the basic concept of objects. I'll refer to these columns in later columns as we develop working scripts.
Once I've introduced the concepts, this column will look at how to use VBScript to perform common tasks by first introducing the task to accomplish and then walking you through the code. The tasks I'll illustrate are based on real tasks, so I encourage you to send me questions, with a couple of caveats: This column can't provide scripts for every occasion, and I can't provide extensive debugging support through e-mail.
I encourage you to think of this column as a serialized book that can respond to your questions. If you need something clarified, ask. You're probably not the only person with that question. You can e-mail your questions to firstname.lastname@example.org.
The main reason to script is to automate repetitive tasks such as changing a password for a user. Scripting tasks let you perform them quickly and consistently and even allow you to delegate tasks to inexperienced administrators. If you're only going to perform a task once, then scripting it doesn't usually make any sense -- by the time you've scripted it, you could be done.
Scripting can also provide functionality that's not available through the graphical interface. The operating system can do many things that the GUI doesn't support, just because including everything in a dialog box would clutter the GUI into an unreadable mess. Actually, though, those features are available -- you just have to make them accessible.
Command-line tools found in the OS or as part of the Resource Kit are an alternative to scripting, but they're not as flexible as the more modular VBScript. You don't have to choose between existing command-line tools and the ones you create; however, it's possible to call on existing programs from a script.
I'm going to begin by defining some key terms. I will also cover scripting hosts, how to interpret scripts and the kinds of data types you will use.
Beginner's guide to scripting
Objects, properties and methods
Scripting hosts and the interpreter
How are scripts interpreted?
Data types you'll use
Objects, properties and methodsObjects are at the core of most scripts. Objects represent parts of the operating system, or user accounts, or parts of the computer, or... you get the idea -- they're a way of representing anything that you can manipulate with the script.
Objects are nouns. Adjectives describe nouns; properties describe objects. Object properties may be a printer's name, a network card's IP address or whether DHCP is enabled for a particular network card. In the same vein, verbs manipulate nouns; and methods manipulate objects. Methods are less common; relatively few objects have true methods -- but one method could be telling a computer to reboot. "Reboot" would be the method.
We'll talk more about kinds of objects and where they come from in next month's column (the second half of defining terms). But, for now, walk away with this: Your scripts will often deal with objects, and to manipulate those objects you will change or read their properties. And, less frequently, you will use object methods to take some action.
Scripting hosts and the interpreterYou get to objects through the scripting host. A scripting host is the operating environment for a script. Windows doesn't understand VBScript -- it needs an interpreter.
When Windows encounters a file with a recognized scripting extension, the OS passes the script to the scripting host. The scripting host interprets the script, then passes the script's message to Windows for execution. A scripting host doesn't understand all scripts; it understands only the ones written in languages -- script engines -- that the host supports.
Windows has two scripting hosts: Microsoft Internet Explorer (MSIE) and Windows Scripting Host (WSH). This column will focus on WSH and the VBScript script engine.
How are scripts interpreted?Each line in a script is a statement that tells the computer what to do next.
Executable statements, the statements that have some result, usually have a simple verb-object form. A conditional statement would outline the conditions under which the verb-object combination applies.
Nonexecutable portions of a script are called comments; they're preceded with the word "Rem" or an apostrophe. Comments document the script for future reference. You can also add Rem or an apostrophe to the beginning of an executable line to disable that line for debugging purposes.
The scripting host interprets lines of code from left to right and top to bottom, so you can, for example, gather information in line 10 of the script, then manipulate that information in line 30. Procedures, collections of statements that run only when the script calls them, are the exception to this rule. They don't run until and unless they're called, and then they execute immediately, regardless of their physical position in the script.
Data types you'll use
VBScript recognizes four types of data: numbers, strings, dates and times, and Boolean statements. Numbers are, well, numbers such as 2 or 9458. Strings are any combination of characters enclosed within quotation marks. Date and time information must be within octothorps (#) and follow VBScript's conventions for date and time information. Boolean statements are either TRUE or FALSE, as in x VBScript sees all four of these data types as subsets of a larger data type, called type variant, which can contain any kind of data. This means that you don't have to tell VBScript what type of data you're feeding it. (It will try to guess, so sometimes you will need to be specific. I'll talk about how to specify the data type -- for example, telling VBScript to interpret "45" as text, not a number -- next month.) Groups of like data are called arrays. You could hard-code the data you're working with into a script, but to make it easier, VBScript supports a structure called a variable, which can be populated by any data type required. Variables can, and often do, change value in the course of a script. Another structure that abstracts the interface to the data is called a constant. Unlike variables, constants always represent the same data, such as a button saying "No." In my next column, I'll finish explaining the background you will need to create working scripts of your own.
Beginner's guide to scripting: SummaryIf you're new to scripting, I've probably hit you with enough information for now.
VBScript sees all four of these data types as subsets of a larger data type, called type variant, which can contain any kind of data. This means that you don't have to tell VBScript what type of data you're feeding it. (It will try to guess, so sometimes you will need to be specific. I'll talk about how to specify the data type -- for example, telling VBScript to interpret "45" as text, not a number -- next month.) Groups of like data are called arrays.
You could hard-code the data you're working with into a script, but to make it easier, VBScript supports a structure called a variable, which can be populated by any data type required. Variables can, and often do, change value in the course of a script. Another structure that abstracts the interface to the data is called a constant. Unlike variables, constants always represent the same data, such as a button saying "No."
In my next column, I'll finish explaining the background you will need to create working scripts of your own.