Modifying an Existing ScriptNow 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.")
Note
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 InformationThe 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 Modifying the Reference InformationBecause 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:
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
Modifying the Worker InformationYou 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:
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
Note
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 InformationThe 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:
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
|