Sunteți pe pagina 1din 13

VBScript Modular Programming

Lab
Objectives
In this lab, students will complete the following objectives.

Create a VBScript program using NotePad++.


Use procedures to modularize a VBScript program.
Create a library of procedures.
Run library procedures from a separate VBScript program.

Lab Diagram
During your session you will have access to the following lab configuration.

Connecting to your lab


For this lab, we will only need to connect to Vlab-PC1.

Vlab-PC1

To start simply click on the named Workstation from the device list (located on
the left hand side of the screen) and click Power on in the tools bar. In some
cases the devices may power on automatically.
During the boot-up process an activity indicator will be displayed in the name
tab.

BlackPowered Of
OrangeWorking on your request
GreenReady to access

COMP230_Wk 5_Modular_Lab

Revision Date: 1213

If the remote console is not displayed automatically in the main window (or
popup) click the Connect icon located in the tools bar to start your session.
If the remote console does not appear please try the following option.

Switch between the HTML 5 and Java client versions in the tools bar.

In the event this does not resolve your connectivity problems please visit our
Help / Support pages for additional resolution options.

Task 1: Analyze the Batch Script File


PC_Tests.cmd
Open NotePad++ and use File/Open to view the C:\comp230\PC_Tests.cmd
script file in the Editor window. If the file is not there, it can be extracted from
the Doc Sharing file PC_Tests.zip.

One of the drawbacks of using VBScript instead of a full-featured programming


language such as C++ or Java is the fact that VBScript does not have the
capability to clear the command prompt screen. This is one of the things we
need to do with our menu-driven PC_Tests program each time the menu is redisplayed. Because we cant do this in VBScript, we will do it in a Batch script file.
An listing of the the PC_Tests.cmd Batch script shown below. The script
performs the following tasks.

COMP230_Wk 5_Modular_Lab

Revision Date: 1213

Clear the screen (CLS).


Display the menu using echo commands.
Prompt the User and store the input into choice.
Run the PC_Tests.vbs program and send it the choice value (1..5) as an
argument (//nologo hides the scripting host heading).
When the PC_Tests.vbs program exits and returns to the Batch script, execute a
Pause.
After the user presses a key to end the pause, go to the start of the script to
clear the screen and re-display the menu.
A sample Batch script run of Option 1 is shown below.

COMP230_Wk 5_Modular_Lab

Revision Date: 1213

Task 2: Analyze the Code in the


PC_Tests.vbs Program
1) Open the PC_Tests.vbs program in NotePad++. It contains code that can tell
us many important things about the computer and the Windows Operating
System. Each menu option is represented by a Case in the Select Case
statement. Select Case is an alternate way of coding an if/else statement with
multiple options. Option 1 from the menu is handled by Case 1 in the Select
Case statement. The VBScript code shows how to access the values of Windows
Environmental variables using the WScript.Shell objects
ExpandEnvironmentStrings( ) function. Note that the environmental variables
must be delimited with the % character. Do a search for Windows Environmental
Variables. They can provide a wealth of information about the computer and the
operating system. A Run of this code is shown below (your specific output will
depend on configuration of your computer).

2) Option 2 from the menu is handled by Case 2 in the Select Case statement.
The Case 2 code segment shows the Check System Memory routine. The
somewhat cryptic code shown below utilizes an extremely important tool called
Windows Machine Instrumentation (WMI). WMI allows you to access many
underlying objects and attributes managed by the Windows OS.
The variable strComputers value . refers to the local computer rather than a
remote computer. See where strComputer is used when you are defining
objWMIService. The ExecQuery method is used to send a query (Select *
from Win32_ComputerSystem) to the operating system.

COMP230_Wk 5_Modular_Lab

Revision Date: 1213

Note that the memory size is divided by 1M (1048576) so the answer can be
expressed in Mbytes instead of Bytes. The run is shown below the script code.

3) Option 3 from the menu is handled by Case 3 in the Select Case statement.
The Case 3 code segment shows the Check Operating System Version
routine. Note that this program makes use of the same objWMIService
definition as the last program.
Again we use the ExecQuery method to send a query to the Windows OS. Note,
however, that query has a diferent target (Select * from
Win32_OperatingSystem). Notice also that the ExecQuery method always
returns a collection object colOperatingSystems. An ExecQuery can return
more than one record so we need to handle the display in a For Each/Next loop.

4) Option 4 from the menu is handled by Case 4 in the Select Case statement.
The Case 4 code segment shows the Check Printers Status routine. This
COMP230_Wk 5_Modular_Lab

Revision Date: 1213

routine, like the last two, defines the objWMIService object in exactly the same
way. This time the W32_Printer is queried. A run of the Check Printers Status
routine is shown on the next page.

5) Option 5 is handled by Case 5 in the Select Case statement. The Check


Logical Drive Information routine also uses the objWMIService. The ExecQuery
method sends a query to the W32_LogicalDisk object and the query is modified

COMP230_Wk 5_Modular_Lab

Revision Date: 1213

with Where FreeSpace > 0. The where clause ensures we will not display the
CDROM or DVD drives. The Size and FreeSpace are divided by 1G (1073741824)
to display the sizes in Gbytes.

6) Open a Windows CLI as Administrator and run the PC_Tests.cmd Batch file
(C:\comp230\PC_Tests.cmd <Enter>). The Batch script will clear the screen and
display the PC Tests menu. After a choice is made, the PC_Tests.vbs program is
run using cscript.
7) Run all of the PC Tests and see what happens when you enter incorrect menu
choices. Is there any error-handling incorrect menu choices? Make sure that
everything is functional before you start your Task 3 modifications.

COMP230_Wk 5_Modular_Lab

Revision Date: 1213

Task 3: Make the PC_Tests.vbs Program


Modular
Programs and scripts can be easier to write, modify, and debug when the major
tasks of the program are written as separate identifiable modules. VBScript
allows for two kinds of modules: functions and procedures. Functions are
unique in that they always return a value. Functions are typically used to input a
single value or to perform a calculation. Procedures, however, do not return a
value. Procedures, rather, perform a task.
1) Our first modification to the PC_Tests.vbs will involve re-writing the five tasks
described in Task 2 as separate procedures. Before we get started, Save As the
PC_Tests.vbs program as Mod1_PCTests.vbs.
Cut and copy all of the code for Case 1 in the Select Case statement and
paste it below the Select Case statement. Add a line above this code that reads:
Sub System_Information. After the last line of the pasted code, add the line
End Sub. Go back to Case 1 in the Select Case statement and add the line
Call System_Information. The completed procedure (or SubRoutine) for
System_Information is shown below the modified Select Case statement.
Continue these modifications until each of the PC Test routines have
been re-written as procedures (Subs). Make sure that all lines are properly
indented for the Subs and the Select Case statement.

COMP230_Wk 5_Modular_Lab

Revision Date: 1213

2) Before you do a final save of Mod1_PCTests.vbs and close the script, you
need to do one more modification. You need to add error-handling in the VBScript
program for incorrect menu choices in the Batch script. Add a default Case (case
Else) in the Select Case statement between the Case 5 subroutine call and
the End Select statement. The error-handling routine Beeps the speaker twice
(sending an ASCII code 7 to screen two times) and displays an appropriate error
message.

Save your Mod1_PCTests.vbs program and open the PC_Tests.cmd Batch


script in NotePad++. Change the script to be executed from PC_Tests.vbs to
Mod1_PCTests.vbs as shown below. Save the new Batch script as
Mod1_PCTests.cmd.

3) Open the Windows CLI (Command Prompt) in the C:\comp230 directory and
run the Mod1_PCTests.cmd Batch script which will call the Mod1_PCTests.vbs
program. In your run, show error-handling and successful execution of all five PC
Tests.
Note: Please edit your Run for each menu option so that the Menu is
shown only once for option 1 and thereafter just show the selected
option choice and the subroutine output. To save space in the labreport document, please edit your run to show only the first Printer and
the first Hard Drive. You must also show error-handling in your Run. A
Sample Edited Run is shown at the end of the lab.
Copy your Mod1_PCTests.vbs program and your complete edited Run in your lab-report
document in the specified textboxes.

Task 4: Create a Library of PC Test


Functions Accessible by any VBScript
Most high-level programming languages allow for the creation of libraries of
common used Procedures and Functions. Although, creating libraries is not an
COMP230_Wk 5_Modular_Lab

Revision Date: 1213

explicit capability of Windows Scripting Host (WSH), it is possible to access


procedures and functions defined in one VBScript program from another VBScript
program. As you develop functions and procedures that you would like to re-use
in other programs, this can be a useful thing to know.
1) Our second modifcation of our PC_Tests VBScript program will involve
creating a pseudo-library of PC Test routines and calling them from a very
simplified VBScript program.
2) Open the Mod1_PCTests.vbs program in NotePad++ and immediately save it
as Mod2_PCTests.vbs.
3) Highlight all of the procedures you have defined with your mouse and press
<Ctrl>X to cut them from the script and put them in the clipboard.
4) Select File/New to open a blank script. Use <Ctrl>V to paste the procedures
(in the clipboard) into this new script file. Insert an appropriate header for this at
the beginning of this library script and save it as PCT_Library.vbs.
5) Now that we have our library, we need to find a way to access it from our
simplified Mod2_PCTests.vbs program. Add the following code just above the
Select Case statement. Line 5 defines a Scripting.FileSystem object called fso.
Line 6 opens the library script that we saved earlier and assigns the file object to
vbsLib. Line 7 reads the library script file into a global string variable called
librarySubs. Lines 8, 9, and 10 close the files and reset the vbsLib and fso
objects by assigning them to Nothing.

6) The only other change we need to make Mod2_PCTests.vbs is one that will
give us access to the library string. That line is shown below. This should be the
last line before the beginning of the Select Case statement. From the previous
step, we know that all of the PCT_Library.vbs file content has been loaded into
the string variable librarySubs. The command ExecuteGlobal librarySubs on
line 11 gives us access to all of the procedures and functions contained within
that string as if they were directly implemented within the Mod2_PCTests.vbs
script.

7) The executable code for Mod2_PCTests.vbs has been greatly simplified by


creating and using the PCT_Library.vbs file as a container for the procedures

COMP230_Wk 5_Modular_Lab

10

Revision Date: 1213

we want to use. The final code for Mod2_PCTest.vbs (minus the programmer
header) is shown below.

Save your Mod2_PCTests.vbs program.

Task 5: Save Mod2_PCTests, Create


Mod2_PCTests.cmd, Run Program, and
Submit Final Codes and Runs
1) Open the Mod1_PCTests.cmd Batch script in NotePad++. Change script to
be executed from Mod1_PC_Tests.vbs to Mod2_PCTests.vbs as shown below.
Save the new Batch script as Mod2_PCTests.cmd.

2) Open the Windows CLI as Administrator and run the Modular PC Tests script
Mod2_PCTests.vbs from the Mod2_PCTests.cmd Batch scripts.
Note: If you receive an error message on the ExecuteGlobal
librarySubs line, you need to check the PCT_Library.vbs file for errors.
3) In your run, show error-handling and successful execution of all five PC Tests.
Note: Please edit your Run for each menu option so that the Menu is shown only
once for option 1 and thereafter just show the selected option choice and the

COMP230_Wk 5_Modular_Lab

11

Revision Date: 1213

subroutine output. To save space in the lab-report document, please edit your
run to show only the first Printer and the first Hard Drive. You must also show
error-handling in your Run. A Sample Edited Run is shown at the end of the lab.
Copy your Mod2_PCTests.vbs program and your complete edited Run in your lab-report
document in the specified textboxes.

Sample Run of Mod1_PCTests and Mod2_PCTests:

COMP230_Wk 5_Modular_Lab

12

Revision Date: 1213

COMP230_Wk 5_Modular_Lab

13

Revision Date: 1213