Creating a Script Template

Microsoft® Windows® 2000 Scripting Guide

Templates provide a framework for entering data. When you order office supplies, you probably fill out a form of some kind. Rather than obligate you to guess at the information, and the format, required, the form will be presented to you as a template, a framework that tells you what information is required and where this information must be placed.

Script writers also need to know what information is required when they write a script, and they need to know to know how this information must be presented. A script template can help script writers adhere to organizational standards.

A script template contains placeholders predefined to fit a standard format. For example, your template might include placeholders for the script header, the initialization section, the parsing of command-line arguments, and other key sections. You might create several different templates for various script types. For example, you might have a template for Active Directory Service Interfaces (ADSI) scripts that includes all the predefined constants and procedures required to connect to your directory service.

The following is a sample script template:

'*******************************************************************************
'* Microsoft Windows 2000 Scripting Guide
'* Chapter Name:   CHAPTER_NAME_GOES_HERE
'* Sample Listing: CHAPTER_NUMBER_GOES_HERE.LISTING_NUMBER_GOES_HERE
'* File:           NAME_OF_SCRIPT_FILE_GOES_HERE
'* Purpose:        This script demonstrates ADDITIONAL_DESCRIPTION_GOES_HERE
'* Usage:          COMMAND_LINE_USAGE_GOES_HERE
'* Version:        1.0 (MONTH_GOES_HERE 2002)
'* Technology:     VBSCRIPT_WMI_ADSI_ETC._GOES_HERE
'* Requirements:   Windows 2000
'*                 Windows Script Host 5.6 - CSCRIPT.EXE_OR_WSCRIPT.EXE
'* Copyright (C) 2002 Microsoft Corporation
'* History:
'*******************************************************************************

Option Explicit

'*******************
'* Define Constants
'*******************

'********************
'* Declare variables
'********************

These templates are particularly useful when they are available through the context menu in Windows Explorer. In Windows Explorer, you can right-click a blank area in the window, point to New, and then choose from various predefined document types (including Text Document, Bitmap Image, and Wave Sound). When you click one of these document types, a new, blank document of that type is created for you; for example, if you click Text Document, a new text-only document named New Text Document.txt is created for you.

By creating a script template and adding it to the Windows Explorer context menu, you can create a new script, based on the template, simply by clicking a blank area within Windows Explorer, pointing to New, and then clicking the template. By making a template readily, and universally, accessible, you make it more likely that system administrators and other script writers within your organization will use it.

To create a template and add it to the Windows Explorer context menu:

  1. Develop the format in Notepad, and then save the file in the systemroot\ShellExt folder. In the example shown, the file is saved as C:\Windows\System32\ShellExt\Template.vbs.

  2. After you save the file, add the template to the New context menu. When the template is added to the New context menu, you can create a new VBScript file based on the template by right-clicking anywhere inside a folder, pointing to New, and then clicking VBScript Script File.

  3. To add your template to the context menu, create a new registry entry referencing the template you just created, as shown in Listing 18.1.

Listing 18.1 Creating a Registry Entry

  
1
2
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite "HKCR\.VBS\ShellNew\FileName","template.vbs"