Registry Reading
The WshShellobject is often used to read the local registry. To read or manipulate a remote registry, you must use Windows Management Instrumentation (WMI.) Assuming the user executing the script has the appropriate permissions, you can easily read and write information
from the registry. Here is a quick example of reading owner and product information from the
local registry.
dim objShell
Set objShell=CreateObject("wscript.shell")
strRegisteredUser=objShell.RegRead("HKLM\Software\Microsoft\" &_
"Windows NT\CurrentVersion\RegisteredOwner")
strProduct=objShell.RegRead("HKLM\Software\Microsoft\Windows NT\" &_
"CurrentVersion\ProductName")
WScript.Echo strRegisteredUser & " is running " & strProduct
Program Launching
You will often need to call another script or program from your main administrative script.
Fortunately, the WshShellobject makes this possible, as shown in Listing 1-2.
WshShell RunSample
dim objShell
'Window style
Const WINDOWHIDDEN=0
Const WINDOWNORMAL=1
Const WINDOWMINIMIZE=2
Const WINDOWMAXIMIZE=3
6 Part I: The Basics of Advanced Windows Scripting
Set objShell=CreateObject("wscript.shell")
'enter in full path to command or script if not in %Systemroot%
strCommand="Notepad"
objShell.Run strCommand,WINDOWNORMAL,True
'this line won't echo until previous command finishes
WScript.Echo "Script complete"
The important parameters are the window style and whether the command
should wait before continuing with script execution. In this example, we set up constants for
typical window styles. (Again, refer to the Windows Script Host 5.6 documentation for additional window styles.) You will likely want to run a program or script and hide it from the user.
Depending on the program or script, you can do this by setting the window type parameter to 0.
If you want execution in your main script to wait for the command to finish, set the WaitOnReturnvariable to TRUE. In Listing 1-2, the line of code that displays Script Completewon’t
execute until the command launched by the WshShellobject has completed.
Another way to execute a command is with the Execmethod. This technique is especially useful for parsing out the results of a command-line tool or utility. Often, you might find yourself
developing a script that could use the output of another command for reporting or as parameters. Listing 1-3 takes the output of a Dircommand that displays all executables, and modifies it so that it displays only the directory name and file information. You must run this script
from a command prompt using CScript.
WshShell ExecSample: Dir
Dim objShell,objExec
Set objShell=CreateObject("wscript.shell")
'command to Execute
strCommand="cmd /c DIR c:\*.exe /s"
'text to look for in the output
strFind=".exe"
'Create Exec object
Set objExec=objShell.Exec(strCommand)
'parse output and only display lines With
'target text
Do While objExec.StdOut.AtEndOfStream<>True
strLine=objExec.StdOut.ReadLine
'parse out lines
If InStr(strLine,"Directory") Then
WScript.Echo Trim(strLine)
Elseif InStr(strLine,strFind) Then
WScript.EchovbTab & strLine
End If
Loop
At run time, the script passes the specified command to the WshShellobject and executes it.
The output of that command is then redirected to a new object, called objExecin our script.
With this object, we can leverage the power of StdOutand manipulate the data that would
ordinarily be written in the command prompt window. As long as the command is running,
the AtEndOfStreamproperty will be FALSE. Because we want to display specific information
from what would generally be a lengthy output, we set a variable, strLine, to the value of the
next line of StdOut. Then we can use the InStrfunction to find strings of interest. If there is a
match, the line is displayed. In Listing 1-3, we want to display the directory name as well as
each line that includes the file name.
Tip With StdOut, you can use any text stream property from the FileSystemObjectlibrary,
such as ReadLine, SkipLine, and AtEndOfStream.
WshShell ExecSample: Nslookup
Dim objShell,objExec
Set objShell=CreateObject("wscript.shell")
'command to execute
strCommand="Nslookup www.microsoft.com"
'Create Exec object
Set objExec=objShell.Exec(strCommand)
'skip lines that contain information about our DNS
'server
objExec.StdOut.SkipLine
objExec.StdOut.SkipLine
Do While objExec.StdOut.AtEndOfStream<>True
strLine=objExec.StdOut.ReadLine
WScript.Echo strLine
Loop
WScript.Quit
demonstrates another way to use the Execmethod: executing an Nslookupcommand. Our script parses out the lines of interest, namely the IP address of the specified name,
and neatly displays them. The script simply skips the first two lines of any Nslookupoutput
that contains the DNS server name and IP address.
Tip Although not an absolute requirement, you will find it easier and neater to run scripts
that take advantage of StdOutand StdInfrom the command line by using CScript. For example,
if you run the script in Listing 1-4 by double-clicking it, you will see a few blank popup windows. If you run the script from a command prompt by using CScript, you will get cleaner looking results.
No comments:
Post a Comment