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"