• Aucun résultat trouvé

Modifying the Output information

The Output information section of the script takes what you’ve learned from the regis­

try 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 infor­

mation but not too much time formatting the data we get. You’ll beef up your knowl­

edge 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 script—the 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 quota­

tion 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 information 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 informa­

tion 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 we’re listing all the user names in our domain—which has 11,000 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. (Yes—that’s right—with 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 addi­

tion, 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 on the Options tab, and select the Quick Edit Mode box. Next, choose Save Prop­

erties 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 fea­

ture 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 enter­

prise 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.

Summary

In this chapter, you looked at your first script. Recall that the script is broken into four parts: the Header information, the Reference information, the Worker information, and the Output information sections. You also looked at variables and how to make the script

engine aware of their presence. We learned how to read from the registry and how to get information out to the user. Finally, you looked at modifying scripts and learned to deter-mine which parts of the script can be re-used and which parts need to be re-created.