You are currently browsing the Gene Laisne's blog blog archives for June, 2009.



vbscript starting point…

By Gene Laisne

Everybody needs a starting point. Now, I’m not going to teach you HOW to write scripts, think of this as more of a repository of useful scripts and script parts that I have gathered over the years.

First, I recomend using Edit Plus as your editor. It does great syntax highlighting, and it uses regular expressions in it’s find, find and replace and other functions. Once you get used to it you’ll wonder how you ever worked without it.

One great tool is the Microsoft script repository. Lots of little scripts to get you going. Also, you can look around the scriptcenter for some help.

For me, when I need to make a script I try to do it in pieces. I’ve built some functions and classes along with script templates where all I have to do is drop a few things in here or there and assemble the script. Makes things much easier.

Here are some of my basics. I start with a vbscript template. This gets me started. There are a collection of variables, objects and constants that I use on a reglar basis that are constant through out all my scripts. I recomend using a naming convention and keeping it as constant as possible. For me constants are in all caps, and all of my objects start with ‘obj’ as you’ll see in the file. Also, my scripts have a header so I can keep track of changes that may have been made.


The next thing I do is drop in my help() function. This function is designed to require switches and if a user enters “/?”, “-help”, “–help” or any of a few other “help switches” they get the help output just like they were using a regular dos program.

There are two parts to this function. First, there is the help() function itself, which will need to be editied for you collection of switches, and then there is the parsing section of this template. The help() function, I always put at the end of the script, in fact, my scripts are all organized like this:

  1. Header
  2. Global variables (I know, you’re not supposed to use global variables, but this is scripting… give me a break. Besides, if you careful, and know what you’re doing this is fine.)
  3. Script
  4. Classes
  5. Functions

Now, the parsing portion of the scripts is setup to go through each argument to the script. The arguments are in an array so if you using a script that is exactly like the dos command ‘copy” then you can make the first argument ‘source’ and the second argument ‘destination.’  If, on the other hand, you were building a script like ‘gpresult,’ you would use the current argument to get the current switch, and the next argument (current index + 1) to specify the parameter.

objArgs is the argument object, this has all the arguments.
argCounter specifies the current index in the arguments.
So, “objArgs(argCounter)” references the current argument.
if “objArgs(argCounter)” is the current argument, then “objArgs(argCounter + 1) is the next argument.

So, if we wanted to use “/s <system name>” as an argument for our script, then in our select statement, we would have:

Select Case objArgs(argCounter)
    Case “/s”
        strSystem = objArgs(argCounter + 1)
End Select

So, that’s the start of any good script. See the attached files.