Microsoft WSH and VBScript Programming for the Absolute Beginner Part 7 - Pdf 16

40
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Object Description
WshNamed This object provides access to a set of named command-line arguments.
Properties:
Item and Length.
Methods:
Count() and Exists().
WshUnnamed This object provides access to a set of unnamed command-line arguments.
Properties:
Item and Length.
Methods:
Count().
WshController This object provides the capability to create a remote script process.
Properties: This object does not support any properties.
Methods:
CreateScript.
WshRemote This object provides the capability to administer remote computer systems
using scripts over a network.
Properties:
Status and Error.
Methods:
Execute() and Terminate().
WshRemoteError This object provides access to information on errors produced by remote scripts.
Properties:
Description, Line, Character, SourceText, Source, and Number.
Methods: This object does not support any methods.
WshNetwork This object provides access to a number of different network resources such as
network printers and drives.
Properties:
ComputerName, UserDomain, and UserName.

computers.
More than three dozen properties are associated with various WSH objects. In many cases,
properties are associated with more than one object. Refer to Table 2.2 to see which proper-
ties are associated with which objects.
Table 2.3 provides a complete review of WSH object properties.
41
Chapter 2 • Overview of the Windows Script Host
Object Description
WshUrlShortcut This object provides scripts with methods and properties for creating and
manipulating URL shortcuts.
Properties:
FullName and TargetPath.
Method:
Save().
WshEnvironment This object provides access to Windows environmental variables.
Properties:
Item and Length.
Methods:
Remove() and Count().
WshSpecialFolders This object provides access to special Windows folders that allow scripts to
configure the Start menu, desktop, Quick Launch Toolbar, and other special
Windows folders.
Properties:
Item.
Methods:
Count().
WshScriptExec This object provides access to error information from scripts run using the
Exec method.
Properties:
Status, StdOut, StdIn, and StdErr.

Number Provides access to an error number.
Path Returns the location of the folder where the CScript or WScript execution
hosts reside.
ProcessID Retrieves the process ID (PID) for a process started using the WshScriptExec
object.
ScriptFullName Returns an executing script’s path.
ScriptName Returns the name of the executing script.
Source Retrieves the identity of the object that caused a script error.
TABLE 2.3 WSH OBJECT P ROPERTIES
Working with Object Properties
Now let’s take a look at an example of a VBScript that demonstrates how to instantiate an
instance of the
WshNetwork object and access its properties. The script is called NetInfo.vbs
and is as follows:
Set WshNtwk = WScript.CreateObject(“WScript.Network”)
PropertyInfo = “User Domain” & vbTab & “= “ & WshNtwk.UserDomain & _
vbCrLf & “Computer Name” & vbTab & “= “ & WshNtwk.ComputerName & _
vbCrLf & _ “User Name” & vbTab & “= “ & WshNtwk.UserName & vbCrLf
MsgBox PropertyInfo, vbOkOnly , “WshNtwk Properties Example”
As you can see, it isn’t a very big script. It begins by using a Set statement to create an
instance of the
WshNetwork object, which is associated with a variable name of WshNtwk. After
you have established an instance of the
WshNetwork object in this manner, you can reference
the object’s properties and methods using its variable name assignment.
43
Chapter 2 • Overview of the Windows Script Host
Property Description
SourceText Retrieves the source code that created the error.
SpecialFolders Provides access to the Windows Start menu and desktop folders.

• WshNetwork.UserDomain. The name of the domain into which the person running
the script is logged in.
• WshNetwork.ComputerName. The name of the computer on which the script is
being executed.
• WshNetwork.UserName. The username of the person who ran the script.
To improve the presentation of the message, I formatted it using the VBScript
vbTab and
vbCrLf constants. The vbTab constant is used to line up the output at the point of the equals
sign. The
vbCrLf constant is used to execute a line feed and carriage return at the end of
each line of output.
The last thing that the script does is display the message using the following statement:
MsgBox PropertyInfo, vbOkOnly , “WshNetwork Properties Example”
MsgBox()
is a built-in VBScript function that displays a
text message in a pop-up dialog.
PropertyInfo is a vari-
able that I used to store the output message.
VbOkOnly
is a VBScript constant that tells the MsgBox() function
to only display the OK button in the pop-up dialog. The
last part of the previous statement is a message that
will be displayed in the pop-up dialog ’s title bar. If
you save and run this script yourself, you should see a
pop-up dialog similar to the one shown in Figure 2.6.
44
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Definition
The Set statement is used to create
a reference to a specified object.

effects on the operation of the computer and may potentially prevent it from
being able to start. I strongly recommend that, unless you’re very sure of what
you are doing, you never attempt to modify the Registry, either manually or by
using a script.
Table 2.4 provides a complete review of WSH object methods.
TRAP
TRAP
45
Chapter 2 • Overview of the Windows Script Host
Figure 2.6
A pop-up dialog
displaying
properties
associated with
the WshNetwork
object.
46
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Method Description
AddPrinterConnection() Creates printer mappings.
AddWindowsPrinterConnection() Creates a new printer connection.
AppActivate() Activates the targeted application window.
Close() Terminates or ends an open data stream.
ConnectObject() Establishes a connection to an object.
Count Retrieves the number of switches found in the
WshNamed and WshUnnamed objects.
CreateObject() Creates a new instance of an object.
CreateScript() Instantiates a WshRemote object representing a script
that is running remotely.
CreateShortcut() Creates a Windows shortcut.

47
Chapter 2 • Overview of the Windows Script Host
Method Description
ReadLine() Retrieves a string containing an entire line of data from the input
stream.
RegDelete() Deletes a Registry key or value.
RegRead() Retrieves a Registry key or value.
RegWrite() Creates a Registry key or value.
Remove() Deletes the specified environmental variable.
RemoveNetworkDrive() Deletes the connection to the specified network drive.
RemovePrinterConnection() Deletes the connection to the specified network printer.
Run() Starts a new process.
Save() Saves a shortcut.
SendKeys() Emulates keystrokes and sends typed data to a specified window.
SetDefaultPrinter() Establishes a default Windows printer.
ShowUsage() Retrieves information regarding the way that a script is
supposed to be executed.
Skip() Skips x number of characters when reading from the input stream.
SkipLine() Skips an entire line when reading from the input stream.
Sleep() Pauses script execution for x number of seconds.
Terminate() Stops a process started by Exec().
Write() Places a string in the output stream.
WriteBlankLines() Places a blank in the output stream.
WriteLine() Places a string in the output stream.
TABLE 2.4 WSH OBJECT M ETHODS (CONTINUED)
48
The WshShell object provides access to a number of Windows resources, including
• The Windows application log
• The Windows Registry
• Any Windows command-line command

The next line allows the user to type in a text message and stores it in a variable called
MsgText.
The last line of this script executes the
WshShell object’s Run() method, passing it the NET
SEND
command, the name of the recipient, and the message to be sent.
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Open your script editor and type in the script as just shown, and then save it as Messenger.vbs.
Run the script and you’ll see a pop-up dialog like the one in Figure 2.7, asking for the user-
name or computer name of the recipient.
Type the required information and click on OK. The pop-up dialog shown in Figure 2.8
appears. Type the message you want to send and then click OK.
Within a few moments, your message appears on the recipient’s screen, as shown in Figure 2.9.
In the second example, you’ll learn how to use the
WshShell object’s LogEvent() method to
write a message to the Windows event log. The Windows event log is accessed differently,
depending on which version of Windows you use. For example, on Windows 2000 and
Windows XP, you can click Start and then right-click My Computer and select Manage to
open the Computer Management console where the Event Viewer utility or snap-in resides.
To view the application event log, expand the Event Viewer node and select Application, as
shown in Figure 2.10. Double-click on an event entry in the event log to examine it.
49
Chapter 2 • Overview of the Windows Script Host
Figure 2.7
Type the
username or
computer name
to which you
want to send a
message.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status