Previous Page
Next Page

Modifying an Existing Script

Now that your script is fully documented, you can modify it to pull in additional information. Thus far, you have learned to retrieve the active computer name, the host name, and the computer name. (Actually, these names could be different in certain situations, so this script really is useful.) What kind of information could you be interested in retrieving at this juncture? Look at Table 1-1 for some ideas. (Notice in Table 1-1 that the registry keys are spelled out completelyHKEY_LOCAL_MACHINE, for instanceand the script you worked on earlier was abbreviated HKLM. VBScript allows you to reference the registry using several forms. These forms are covered in depth in Chapter 17, "Working with the Registry.")

Table 1-1. Useful registry keys for script writers

Information

Location

Service information

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

User name used to log on to the domain

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Logon User Name

Microsoft Exchange 2000 domain information

HKEY_CURRENT_USER\Software\Microsoft\Exchange\LogonDomain

Exchange 2000 domain user information

HKEY_CURRENT_USER\Software\Microsoft\Exchange\UserName

Group Policy server

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy\History\DCName

User's home directory

HKEY_CURRENT_USER\Volatile Environment\HomeShare

The server that authenticated the currently logged-on user

HKEY_CURRENT_USER\Volatile Environment\LOGONSERVER

The DNS domain name of the currently logged-on user

HKEY_CURRENT_USER\Volatile Environment\USERDNSDOMAIN


Note

Much of the information that you can gather via the registry can be obtained by other approaches, such as using Active Directory Service Interface (ADSI) or Windows Management Instrumentation (WMI) (which you'll learn about in later chapters). These are two other ways you can use the power of VBScript to gather information you need to manage your network. You should be aware of this because the registry is a dynamic environment, and keys get moved around from time to time. Thus, the registry is not always consistent among all machines on the network. For instance, there are obviously differences between Microsoft Windows 95 and Microsoft Windows XP, but there are also differences between Microsoft Windows 2000 and Windows XP, and even between Windows XP and a version of Windows XP that has been upgraded from Microsoft Windows Me, for example. Mining information from sources other than the registry can ensure a more consistent result. If at all possible, only try to read the registry for items that cannot be obtained via other methods.


To modify your script to gather some of the information listed in Table 1-1, you need to make a few changes in each of its four sections. Much of your script will be exactly the same, and a few sections will be similar (meaning that you'll need to change a few names to ensure clarity in your documentation). Now you'll look at each section of your script to see what needs to be changed.

Modifying the Header Information

The first three lines of your script can remain exactly the same. You still want to make sure you specify which variables you plan to use in the script, so leave Option Explicit. You also don't want the script to blow up when a value is absent or some other problem arises, so leave On Error Resume Next in place. In addition, because you're connecting to the registry to read items, you'll need the objShell variable in place. There is really no point in renaming these variables or changing them in any other way. By keeping the same name for objShell, for example, you'll always know its purpose. In this respect, you are developing your own naming convention for your scripts.

Option Explicit
On Error Resume Next
Dim objShell

The first three lines are in place and working fine, so now you need to create variables that you will use for the new registry values you want to read. For this example, we use some (but not all) of the values identified in Table 1-1. These variables are here:

Dim regLogonUserName, regExchangeDomain, regGPServer
Dim regLogonServer, regDNSdomain
Dim LogonUserName, ExchangeDomain, GPServer
Dim LogonServer, DNSdomain

Notice that we use our previous naming convention: We preface with reg all names of variables that will hold registry keys, and we leave reg off the names of all variables that will hold the information contained in the registry keys. (The variable item names are the same except for reg.)

Just the Steps

To modify the header information

1.
Open Notepad.

2.
Ensure Option Explicit is listed on the first non-commented line.

3.
Ensure On Error Resume Next is listed.

4.
Delete variables that are not required.

5.
Add variables for new information.

6.
Add comments describing use of the newly added variables.

7.
Save the script with a new name.


Modifying the Reference Information

Because you are changing the registry keys you will pull information from, you'll need to completely replace the Reference information section. The good news is that the format for the section is exactly the same. The pattern looks like this:

Variable name

=

Registry key in quotation marks

regLogonUserName

=

"HKEY_CURRENT_USER\Software\Microsoft\" & _"Windows\CurrentVersion\Explorer\Logon User Name"


There are three parts of the script involved in reading a registry key, and all the information we want to obtain can be easily modified by changing the assignment of values to the variable names listed in the preceding syntax example. In addition, because you listed all the variable names we want to use to hold the registry keys in the Header information section of the script, you can simply cut and paste the variables into the Reference information section. In the next listing, you remove the Dim portion and the commas and place each variable name on a separate line. You will start with the code listed below:

Dim regLogonUserName, regExchangeDomain, regGPServer
Dim regLogonServer, regDNSdomain

Once you have finished cleaning up the variable names, your resulting code will look like Figure 1-3.

Figure 1-3. Using Notepad to speed script modification


After the variable names and the equal signs are inserted, add each registry key and enclose it in quotation marks. Remember to use the copy key feature of Regedit. Once all the registry keys are pasted into the script, the modified Reference information section looks like the following listing. Remember that the ampersand and underscore are used to indicate line continuation and are included here for readability. I also include them in production scripts to avoid having to scroll to the right while revising code.

regLogonUserName = "HKEY_CURRENT_USER\Software\Microsoft\" & _
    "Windows\CurrentVersion\Explorer\Logon User Name"
regExchangeDomain = "HKEY_CURRENT_USER\Software\Microsoft\" & _
    "Exchange\LogonDomain"
regGPServer = "HKEY_CURRENT_USER\Software\Microsoft\Windows\" & _
    "CurrentVersion\Group Policy\History\DCName"
regLogonServer = "HKEY_CURRENT_USER\Volatile Environment\" & _
    "LOGONSERVER"
regDNSdomain = "HKEY_CURRENT_USER\Volatile Environment\" & _
    "USERDNSDOMAIN"

Just the Steps

To modify the reference information

1.
Open Notepad.

2.
Copy the Dim section of the header information.

3.
Paste the Dim section from step 2 into a new Notepad file.

4.
From the Edit menu, select Replace to display the Replace dialog box. In the Find What box, type Dim. Do not type anything in the Replace With box. This will erase all occurrences of the word Dim.

5.
Place each variable on a separate line and remove the commas.

6.
Open Regedit and locate the desired registry keys.

7.
Using the Copy Key Name feature, paste the key after each variable name.

8.
Ensure the variable name is separated from the registry key name with an equal sign.

9.
Ensure the registry key name is enclosed in quotation marks.

10.
Save the script.


Modifying the Worker Information

You are halfway through creating the new script. The first line in the Worker information section of the script is fine and does not need to be changed.

Set objShell = CreateObject("WScript.Shell")

Notice that same two variables listed in the third line of the Header information section are used here. The challenge now is to modify each line so that it assigns the variables you created without the reg prefixes to the variables you created with the reg prefixes. This command has four parts associated with it:

Variable name

=

Worker

Registry variable in ()

LogonUserName

=

objShell.RegRead

(regLogonUserName)


Here's the entire Worker information section of the new script:

LogonUserName = objShell.RegRead(regLogonUserName)
ExchangeDomain = objShell.RegRead(regExchangeDomain)
GPServer = objShell.RegRead(regGPServer)
LogonServer = objShell.RegRead(regLogonServer)
DNSdomain = objShell.RegRead(regDNSdomain)

The variables were all listed in the Header information section and were copied and pasted on separate lines in this section of the script without the Dim statementsjust as we copied and pasted information for the Reference information section of our script. In the next part of the script, insert the equal sign and the same worker component (you always do this), which in this case is objShell.RegRead. The last part of the script contains the registry variable created in the Reference section enclosed in parentheses. This again can be a really quick cut and paste job from the Reference information section.

Just the Steps

To modify the Worker information section

1.
Open Notepad.

2.
Copy the Dim section of the header information.

3.
Paste the Dim section from step 2 into a new Notepad file.

4.
From the Edit menu, select Replace to display the Replace dialog box. In the Find What box, type Dim. Do not type anything in the Replace With box. This will erase all occurrences of the word Dim.

5.
Place each variable on a separate line and remove the commas.

6.
Paste an equal sign and the worker component objShell.RegRead onto each line.

7.
Paste the appropriate variable from the Reference information section and enclose it in parentheses.

8.
Save the script.


Note

I tend to use the cut and paste feature when working with scripts because some of the variable names I create are a little long. Although the names are typically not case-sensitive, for the most part spelling counts, to rephrase something I learned in first grade. The best way I've found to avoid messing up the script is to copy and paste the variable names between my Header information section and my Worker information section.


After you finish modifying the Worker information section of your script, double-check that all declared variables are in place and that everything else is accounted for. Save your script under a different name if you were editing the DisplayComputerNames script. You could try to run it, but it won't do too well because you need to change the last sectionthe Output information section.

Modifying the Output Information

The Output information section of the script takes what you've learned from the registry and displays it in an easy-to-understand format. This section is what really makes the script usable. It's amazing that we spend a lot of time figuring out how to find information but not too much time formatting the data we get. You'll beef up your knowledge of displaying and writing data quite a bit in later chapters. For now, you'll use WScript.Echo to bounce data back.

You can't really salvage much from the old scriptthe process would be too confusing because you'd have to change every variable that holds information from the registry, as well as all the comments added after the keys. So all you will keep are the WScript.Echo lines. Delete everything after WScript.Echo and start cutting and pasting. Make sure you include every variable name identified in the Worker information section of the script. The syntax for this section is made up of four parts and looks something like this:

Command

Variable

&

Comment

WScript.Echo

LogonUserName

&

"is currently Logged on"


Notice that there's a space after the first quotation mark in the comment section. You include the space because the ampersand is used to glue two phrases together, and VBScript does not add spaces when concatenating lines. Our new code section looks like this:

WScript.Echo LogonUserName & " is currently Logged on"
WScript.Echo ExchangeDomain & " is the current logon domain"
WScript.Echo GPServer & " is the current Group Policy Server"
WScript.Echo LogonServer & " is the current logon server"
WScript.Echo DNSdomain & " is the current DNS domain"

To put this section together, you just cut and paste each variable assigned to a registry key in the Worker information section of the script, add an ampersand, and put quotation marks around whatever text will be echoed out. Later on, you'll use WScript.Echo to troubleshoot problems because it's an excellent way to follow progress in a script.

Just the Steps

To modify the Output section

1.
Open Notepad.

2.
Copy each variable added to the Worker information section.

3.
Paste the variables from step 2 into the Output information section.

4.
Add an ampersand after each variable.

5.
Place quotation marks around any text to be echoed out to the screen.

6.
Paste an equal sign and the worker component objShell.RegRead onto each line.

7.
Preface each line with WScript.Echo.

8.
Save the script.


How to Run Scripts

You can run scripts in several ways on Windows Server 2003, each of which has advantages and disadvantages. Let's look at some of these approaches now.

Double-Clicking a File with a .vbs Extension

By default, when you double-click a file with a .vbs extension, the file runs within an instance of WScript.exe. Therefore, using WScript.Echo in the Output information section of the script results in the cute little pop-up boxes. This might not be a big deal when we're talking about two or three variables, but it can be a real pain when one is listing all the user names in a domain with thousands of users! Perhaps a better alternative is the CScript approach.

CScript

CScript can be thought of as the command-line version of the Windows Scripting Host (Figure 1-4). CScript is nice because you don't have to click any dialog boxes to make the script continue. (Yesthat's rightwith the default Windows Scripting Host, the entire script pauses until you click OK in the dialog box, and then the script waits for you to do the same in each dialog box after that.) In addition, you can pretty easily capture output from CScript because you can enable Quick Edit mode from the command window. To do this, click C:\ in the upper left part of the window, and select Properties from the Action menu. Then click the Options tab and select the Quick Edit Mode box. Next, choose Save Properties For Future Windows Of The Same Title, and you're finished. This feature enables you to highlight text and copy it to the clipboard from the CMD window. Once the data is on the clipboard, you can do everything from pasting the data into Notepad to using the text driver for Microsoft Excel and sorting the data into various cells that you can use to produce graphs. You'll learn more about this feature later in the book.

Figure 1-4. CScript offers many options, which can be set from the command line


Embedding Scripts in Web Pages

You can embed scripts inside Web pages. This has some potential use in the enterprise environment in which users who have access to a particular Web site on the intranet can click a button to launch a particular script. This might be a useful and valid use of VBScript for, say, information gathering or troubleshooting. There are some security concerns, however, which you'll learn about later in the book.

Dragging and Dropping a .vbs File to an Open Command Prompt

You can drag and drop a .vbs file to an open command prompt, which launches the script with the default scripting host. The nice thing about this is that you do not have to type the path to the file because Windows Explorer automatically puts it onto the command prompt line.

Dragging and Dropping a .vbs File to Notepad

You can drag and drop the .vbs file to an open Notepad file with a blank page to automatically open the file and display the text.

Adding Notepad to the SendTo Menu

You can easily edit the script by opening it in Notepad. Just add Notepad to the SendTo menu by going into C:\Documents and Settings\%USERNAME%\SendTo and adding a shortcut to Notepad.exe.



Previous Page
Next Page