Designing the Script
Before you fire up Notepad or your favorite script editor, you need to sit down and figure out exactly what your script will do. This is the best way to answer the question "Where do I start?", which is the most common question you'll have when you start writing your own administrative scripts. By following a specific script design process like the one I'm about to show you, you'll always know exactly where to start, and the script itself will come much easier when you start programming.
Whenever I design a script, I use a three-step process.
Gather facts.
This step lets me document what I know about my environment that will affect the script. I'm simply writing down the various things that my script will need to know, or that I'll need to consider as I write the script. This may include details about how Windows works, specific business requirements, and so forth. Define tasks.
This step lets me define the specific tasks my script will accomplish. I get detailed here, focusing on each tiny step I'd have to perform if I were manually performing what I want my script to do. Outline the script.
This step rolls up what I know and what I want to do into a sort of plain-English version of the script. I list each step I think the script will need to take, along with any related information. This becomes the basis for the script I'll write, and scripting itself becomes a simple matter of translating English into VBScript.
In the next three sections, I'll go through this design process with the IIS log rotation tool that you'll be helping me develop in this chapter. If you'd like to practice, take a few moments and walk through the process yourself before reading my results in the following sections.
Gathering Facts
What do you know about IIS and log files? You need to capture the information that your script will need to operate, such as log file locations, names, and so forth. After giving it some thought, I come up with the following list.
Filenames. IIS log files use a file naming format that's based upon the date. Each log filename starts with the letters "ex," followed by a two-digit year, a two-digit month, and a two-digit day. The log file uses the filename extension .log. Files are stored in C:\Winnt\System32\LogFiles by default, at least on a Windows 2000 system. Windows Server 2003 uses C:\Windows\System32\LogFiles. I can store my archived files anywhere I want, so I'll create a folder named C:\Winnt\LogArchive. I'm assuming a Windows 2000 Server computer; for Windows Server 2003, I'd probably use C:\Windows\LogArchive instead. IIS closes each log file at the end of the day and opens a new one. I probably shouldn't try to move the log file that's currently opened by IIS; I should just go for yesterday's log file, instead. Under the main LogFiles folder, IIS creates a subfolder for each Web site. The first one is named W3Svc, the second is W3Svc2, and so forth. For now, I'll concentrate on the first Web site, which uses W3Svc.
That seems to be all the facts I can think of about log files, so now it's time to figure out exactly what the script needs to do.
Defining Tasks
Scripts can use a graphical user interface, so when I start defining the tasks I need to complete I try to think about how I'd do the task from the Windows command line, instead of through the user interface. For example, when I think about how to perform the log rotation task myself, I come up with the following steps.
Locate the folder that contains the log files. Locate the folder that contains the archived files. Figure out the name of yesterday's log file. Move yesterday's log file into the archive folder. Figure out the name of the log file from 30 days ago. Delete the 30-day-old log file.
It's a simple list of steps, because it's not a complicated task. Note that working from the command line forces me to consider steps like figuring out the filename, which I wouldn't have to do if I was using Explorer. In Explorer, I could just look at the filenames because they would be listed for me. Because scripts cannot "look" at things, the command line more closely represents the way the script itself will need to function.
With the basic steps out of the way, I can start outlining my script.
Outlining the Script
The script outline should be a detailed, English explanation of what the script will do, in a systematic fashion. Use your task list as a starting point for the outline. For the log rotation tool, I come up with the following outline. Note that some of these tasks actually get broken down into subtasks.
Define the location of the log files. Define the location of the archived files. Figure out yesterday's date.   Figure out the name of yesterday's log file.  Move yesterday's log file into the archive folder. Figure out the date from 30 days ago.   Figure out the name of the log file from 30 days ago.  Delete the 30-day-old log file.
Notice the two steps with an asterisk ( ). These are pretty much the same thing: Given a date, give out a matching file log name. This subtask can be broken down as follows.
Start with "ex" as the filename. Append the last two digits of the year. Append a two-digit month. Append a two-digit day. Append ".log".
The steps in the main outline with two asterisks also seem to be related, because they're both somehow calculating a date in the past. I don't readily know how to do a few of these steps in VBScript, such as how to figure out the exact date from 30 days ago. But I'm sure there's a way, so I'll worry about that later. If VBScript doesn't provide an easy way to do it, I can always break it down into a subtask.
|