Introduction to VBScript


VBScript is one of the two scripting languages created by Microsoft. For the scripts in this chapter, VBScript will be used. It is not a replacement for a full programming language such as Visual Basic .NET or Visual C++ but is tailored to provide, in many cases, portable code that can be viewed, modified, and executed on any machine with a VBScript host or interpreter.

Unlike the so-called real programming languages that must be compiled before the system can understand the code, VBScript remains in plain text until actual runtime when the code is interpreted and executed on the system. VBScript commands are not recognized directly by the operating system, so they must be run using a VBScript host or interpreter that can convert the code so that the operating system can execute the commands. VBScript is portable and does not carry a lot of the overhead that can sometimes be associated with compiled applications such as DLL files and such. VBScript files can be run using the Windows Scripting Host (WSH) or be written into a Web page that supports scripting.

The file extension for a VBScript file is .VBS, and this extension is configured by default to run using Wscript.exe. This enables a VBS file to be double-clicked to run just like executable .EXE files. Another option is to run the files in a command-line environment using Cscript.exe.

Visual Basic Script Options

A Visual Basic script is not compiled code, so it must be run within a host or context that can interpret the commands and present them to the operating system so they are executed as desired. You can make sure VBScript code is processed by calling the code using the Windows Scripting Host or adding the code within HTML or ASP Web pages on Web servers that support the VBScript language. Also, before compiling the code, you can add VBScript code to Visual Basic or C-compatible applications to handle certain tasks or functions that can be performed with less code than VBScript.

Windows Scripting Host

Using the Windows Scripting Host, scripts written in VBScript or JScript can be interpreted at runtime and executed on a server or workstation. WSH supports running scripts from the command line using Cscript.exe and supports running scripts within the graphical user interface by using Wscript.exe. Both are part of the Windows Scripting Host program. To see the differences and to dive right into a simple script (recommended in a lab environment) using VBScript, follow these steps:

1.

Before you start creating scripts, create a directory called Scripts on the root of the C drive.

2.

Choose Start, Run. Type notepad.exe and click OK to create a new VBScript. To create the script, type the following code in the Notepad window, pressing Enter after each line:

Dim CurrentTime CurrentTime = time Wscript.echo "The current time is "& CurrentTime & "." 


The Dim command declares a variable called CurrentTime that can be referenced throughout the rest of the code. The next line of code sets the CurrentTime variable to the value of the time function, which will give you the current time. The last command, Wscript.echo, will display the text enclosed in double quotation marks followed by the value of the CurrentTime variable. Notice the & symbol after the text and after the variable; it is used to tell the Wscript.echo command that there is still more information to echo and to continue writing to the same line.

3.

Save the file with a .VBS extension, using a name such as c:\Scripts\VBtime.vbs, and close Notepad.

4.

Choose Start, Run and then type cmd.exe to open a command prompt.

5.

Change directory to the c:\Scripts directory.

6.

Type Wscript.exe VBtime.vbs and press Enter. Note how the output is displayed as a pop-up window while Wscript.exe runs by default in the graphical user interface.

7.

Click OK to close the pop-up window and return to the command prompt.

8.

Type Cscript.exe VBtime.vbs and press Enter. Note how the output is displayed in the command-line interface and does not require user intervention to acknowledge the response the way Wscript.exe does.

The reason the same code returns the information in different ways is inherent to the Wscript.exe and Cscript.exe applications. The output is also directly related to the actual commands being called. For example, modify the VBtime.vbs script as follows, save the file, and run the script using both Wscript.exe and Cscript.exe:

Dim CurrentTime CurrentTime = time MsgBox "The current time is "& CurrentTime & "." 


When this script is run using either Wscript.exe or Cscript.exe, the output is the same: a pop-up window displaying the current time. The reason for this result is that the message box (MsgBox) function is a graphic function, whereas the Wscript.echo command merely echoes output to the current interface.

Note

If a command such as Wscript.echo is used several times in a script, be sure to run the script using Cscript. Otherwise, user intervention will be necessary to close each pop-up window sequentially.


Active Server Pages

Active Server Pages (ASP) running on Windows Server 2003 Internet Information Services (IIS) enable Web developers to include scripting code within dynamic HTML pages. This can be client-side scripting that is downloaded and executed on the end user's machine or server-side scripting that is executed on the back-end server. Recently, because a few problematic viruses have been written using VBScript, many organizations now disable client-side VBScripting on their proxy servers and firewalls. In such cases, client-side scripting may not function correctly, so server-side scripting should be used. By default when a client chooses to view the source code, the script will not show up, only the returned values.

An ASP Web page can also be created using notepad.exe, but many developers use a program such as Microsoft FrontPage to simplify code creation. Different objects, object properties, and methods associated with VBScript can be used within an ASP Web page. For example, using the WSH, you displayed information to the console using Wscript.echo; but when you want to display information in a Web page, you use the response object and the write method of that object. To create an ASP Web page that will display the current time, follow these steps:

1.

Log on to an IIS server. Open Windows Explorer, and under the default Web site directory, create a folder called ASPscripts. Check to ensure that the Anonymous user account has access to this folder. It should have these permissions already because it will be inheriting permissions from the parent folder. The default location will be c:\inetpub\wwwroot.

2.

Choose Start, Run. Type notepad.exe and click OK to create a new VBScript. To create the script, type the following code in the Notepad window:

<HTML> <HEAD> <TITLE> My ASP page Using Vbscript! </TITLE> </HEAD> <BODY> <P> <% DIM CurrentTime CurrentTime = time response.write "The current time is " & CurrentTime & "." %> </P> </BODY> </HTML> 


3.

Save this file as C:\Inetpub\ASPscripts\VBtime.asp on the IIS server.

4.

To test the script, open Internet Explorer on the IIS server and type http://localhost/ASPscripts/VBtime.asp to see the output page, as shown in Figure 23.1.

Figure 23.1. A sample ASP Web page using VBScript.


Note

The Web site created in step 4 should already support Active Server Pages. To enable ASP.NET on this Web site, refer to the Help and Support menu. Also, refer to Chapter 11, "Internet Information Services v6."


The output of this ASP Web page is simple, but it can be extended using both HTML and VBScript commands. Also, you can use different scripting languages in a single ASP Web page if necessary. The code in this ASP is similar to the VBScript file previously written. Using ASP, though, you need to designate the beginning of the script code using <% and end of the code using %>. This will assume that the default scripting language VBScript is being used. To designate a different language such as JScript or JavaScript, refer to ASP documentation.




Microsoft Windows Server 2003 Unleashed(c) R2 Edition
Microsoft Windows Server 2003 Unleashed (R2 Edition)
ISBN: 0672328984
EAN: 2147483647
Year: 2006
Pages: 499

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net