Previous Page
Next Page

Creating Additional Objects

In Chapter 1, we looked at creating objects and we discussed objects, properties, and methods. Recall that to perform anything useful, we need to create an object. In the Script56.chm file on the CD-ROM, we have the scripting SDK documentation. If you look up wshShell.object, you will find the properties and objects provided by this object. WshShell is the actual name of the object, and will result in faster results in the SDK. Of course, we never use the name wshShell in a script, we use wscript.shell. We create the wshShell object by using the following command:

Set objShell = createObject("wscript.shell")

Once we create the wscript.shell object, we have access to the following methods:

Method

Purpose

Run

Runs an external command

Exec

Runs an external command, but provides access to the datastream

appActivate

Brings a specified window to the foreground

sendKeys

Enables you to send keystrokes to the foreground application

CreateShortCut

Creates shortcuts

LogEvent

Writes to the application Event log

RegRead

Reads from the registry

RegWrite

Writes to the registry

RegDelete

Deletes from the registry

PopUp

Displays a pop-up dialog box

ExpandEnvironmentStrings

Parses environmental variables (these variables can be displayed by using the Set command from a CMD prompt)


In the WhileWendLoop.vbs script, we used the Run method to run the command prompt (CMD.exe), to have the computer produce a beep. The echo command tells the CMD program to print out something. Chr(07) tells the script we want to use an ASCII value. ASCII values less than 31 are all non-printing characters, and 07, as you saw in the script, makes a beep. In the CreateAddRemoveShortCut.vbs script, we are going to use the CreateShortCut method to create a shortcut on the desktop. We will also use the specialFolders property to pick up the path to the desktop so we can create a shortcut there. The thing that is special about this particular script is that it supplies values for command line arguments. In this way, we run the control.exe program, which provides access to control panel applets. We then use the arguments property of the shortcut object to supply the command line argument, which then launches the specific control panel applet. Microsoft Knowledge Base article KB192806 (available on support.Microsoft.com) details the names of the control panel applets that can be launched in this manner. Figure 2-2 illustrates the properties that can be set on a shortcut.

Figure 2-2. Shortcut properties assigned via script


CreateAddRemoveShortCut.vbs

Option Explicit
Dim objShell
Dim strDesktop 'pointer to desktop special folder
Dim objShortCut 'used to set properties of the shortcut. Comes from using createShortCut
Dim strTarget
strTarget = "control.exe"
set objShell = CreateObject("WScript.Shell")
strDesktop = objShell.SpecialFolders("Desktop")

set objShortCut = objShell.CreateShortcut(strDesktop & "\AddRemove.lnk")
objShortCut.TargetPath = strTarget
objShortCut.Arguments = "appwiz.cpl"
objShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,21"
objShortCut.description = "addRemove"
objShortCut.Save

If we need to run an external script that provides a capability that is not native in VBScript, then we have two choices: We can use the Run method, or we can use the Exec method. The Run method runs a program when we only need to access the program. The Exec method gives us access to the text stream that is produced by running the command. When we run command line utilities, we will nearly always want to capture the text stream. The RunNetStat.vbs script runs the netstat.exe utility. Netstat.exe -? provides help on using this command, and Microsoft Knowledge base article KB281336 supplies lots of examples for using this awesome tool. The great thing about using Netstat is that it will tell you the process ID of programs on your machine that are listening to Transmission Control Protocol (TCP) ports. It will also tell you the Internet Protocol (IP) address of any connections your machine may have made as well. Try the program below. The script is set up so that you can easily change and run any other command line utility as well. All you do is edit the command variable.

RunNetStat.vbs

Option Explicit 'is used to force the scripter to declare variables
'On Error Resume Next 'is used to tell vbscript to go to the next line if it encounters an
Error
Dim objShell'holds WshShell object
Dim objExecObject'holds what comes back from executing the command
Dim strText'holds the text stream from the exec command.
Dim command 'the command to run

command = "cmd /c netstat -ano"
WScript.Echo "starting program " & Now 'used to mark when program begins
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(command)

Do Until objExecObject.StdOut.AtEndOfStream
  strText = objExecObject.StdOut.ReadAll()
  WScript.Echo strText
Loop
WScript.echo "complete" 'lets me know program is done running


Previous Page
Next Page