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 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 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
on 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.
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 determine
which parts of the script can be re-used and which parts need to be re-created.
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 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 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
on 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.
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 determine
which parts of the script can be re-used and which parts need to be re-created.
0 comments:
Post a Comment
mobile here