VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

"WScript" Runtime Object Hierarchy

This section describes WSH runtime objects: WScript and its properties. A tutorial example is provided on how to use WScript.StdIn and WScript.StdOut.

Like any other scripting environment host, WSH provides several nice runtime objects to allow your VBScript code to interact with the environment:

  • WScript - The root object in the WSH runtime hierarchy to hold other WSH objects. WScript also contains some important properties to access script execution level information, like standard input and output, connection to external COM objects, etc.
  • WScript.Arguments - A WshArguments object representing command line arguments as a collection.
  • WScript.CreateObject("WSHController") - Creating a new WshController object to create and control a new execution thread with another script code.
  • WScript.CreateObject("WSHController").CreateScript(command_line, remote_machine) - Creating new WshRemote object to be launched as new execution thread.
  • WScript.CreateObject("WScript.Network") - Creating a new WshNetwork object to access resources on the network, including the local machine.
  • WScript.CreateObject("WScript.Shell") - Creating a new WshShell object to run commands and manage registry keys on the local machine

Let's try a simple example with the StdIn and StdOut properties of the WScript:

'  StdIn_StdOut.vbs
'- Copyright (c) 2015, HerongYang.com, All Rights Reserved.

   Dim StdIn, StdOut
   Set StdIn = WScript.StdIn
   Set StdOut = WScript.StdOut

   Dim cmd
   Do While True
      StdOut.WriteLine
      StdOut.WriteLine "Herong, what can I do for you?"
      cmd = StdIn.ReadLine
      If cmd = "Nothing" Then Exit Do
      StdOut.WriteLine "Sorry, I am not programmed to do that."
   Loop

Now run it with cscript.exe:

C:\herong\>cscript StdIn_StdOut.vbs

Herong, what can I do for you?
Make me rich
Sorry, I am not programmed to do that.

Herong, what can I do for you?
Make me happy
Sorry, I am not programmed to do that.

Script execution time was exceeded on script 
   "C:\herong\StdIn_StdOut.vbs".
Script execution was terminated.

The code worked nicely. But it was terminated because of the execution time limit. You need to run it with a longer limit specified in the command line, like //T:600 for 10 minutes:

cscript //T:600 StdIn_StdOut.vbs

Note also this example can not be executed with the wscript.exe host, because it does not support StdIn and StdOut properties. Running "wscript StdIn_StdOut.vbs" will result an error: "The handle is invalid"

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

WSH (Windows Script Host)

 What is WSH (Windows Script Host)?

 "csript.exe/wscript.exe" Command Version and Options

 Running VBScript Code Files

"WScript" Runtime Object Hierarchy

 "oShell.Exec(cmd)" - Running System Command with a Script

 WSF - Windows Script File XML Format

 References

 Printable Copy - PDF Version

"WScript" Runtime Object Hierarchy - Updated in 2015, by Dr. Herong Yang