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 OptionsA 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 HostUsing 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:
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 PagesActive 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:
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. |