Monday, 30 June 2014

Registry Reading

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.

Understanding (VBscript) Windows Script Host Basics

Understanding Windows Script Host Basics

We assume you have Microsoft Windows Script Host 5.6 installed. If you aren’t sure, open a 
command prompt and type cscript //logo //?. You should see something like this.
Microsoft (R) Windows Script Host Version 5.6 
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. 
Usage: CScript scriptname.extension [option...] [arguments...] 
Options: 

//B :Batch mode: Suppresses script errors and promptsfrom displaying 
//D :Enable Active Debugging 
//E  :engine Use engine for executing script 
//H  :CScript Changes the default script host to CScript.exe 
//H  :WScript Changes the default script host toWScript.exe (default) 
//I    :Interactive mode (default, opposite of //B) 
//Job :xxxx Execute a WSF job 
//Logo :Display logo (default) 
//Nologo :Prevent logodisplay: No banner will be shown at execution time 
//S      :Save currentcommand line optionsfor this user 
//T      :nn Time outin seconds: Maximum time a script is permitted to run 
//X      :Execute script indebugger 
//U      :Use Unicode for redirected I/O from the console

In the next sections, we’ll cover a few key WSH elements that recur in our scripts throughout 
the book.
Note Many of the scripts and samples in this book require administrator privileges, either 
locally or on remote systems. We assume you will be running any scripts as a local or domain 
administrator. Where appropriate, we will point out how and where to use alternate credentials.
WshShell
The WshShellobject offers a lot of functionality to scripting administrators. You can use it to 
send a message to the user through a popup, read and write to the registry, launch other programs, and more. Let’s take a quick look at this object.

Popup

The Popupmethod displays a graphical message to the user, complete with buttons and icons. 
One advantage to using the Popupmethod instead of the similar MsgBoxfunction (discussed 
later in this chapter) is that you can configure the popup window to dismiss itself automatically after a specified number of seconds. This is ideal when you want to display information 
to a user but you don’t want to rely on t he user to click OK. Listing 1-1 illustrates t he use of t he 
Popupmethod with a sample script.

 WshShell PopupSample

dim wshShell 
set wshShell=CreateObject("wscript.shell") 
'title for popup window 
strTitle="Welcome" 
'compose popup message text 
strMsg="Thank you for logging in." & VbCrLf 
strMsg=strMsg & "It is now " & Now & VbCrLf 
strMsg=strMsg & "Havea nice day." 
'set time to -1 to never dismiss popup window 
wshShell.Popup strMsg,7,strTitle,vbOKOnly+vbInformation

Notice that we use some intrinsic constants, vbOkOnly and vbInformation, as part of our 
popup parameters. These constants display the OK button and the information icon, respectively. These same constants are also used with the MsgBoxfunction. You can find more information about them in the Windows Script Host 5.6 documentation.