Guy's Scripting Ezine 62 - Registry
Contents for Ezine 62 - Registry
I love writing for you scripters. The reason that I enjoy producing these newsletters is because scripters are forgiving, you understand that VBScripts have teething troubles. In fact when a complex script works perfectly first time, it is the source of amazement. On the other hand when a script fails - no one is surprised. The old saying is true: There is no gain without pain.
Before we start I just want to say, 'Take care with the registry'. This week's script will just read the registry. But next week I will have another script, which will write new keys and values, so we all need to understand how the registry is structured.
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information on UMRA.
The purpose of this week's script is straightforward - to read the registry. The logic is simple: create an object, manipulate it with the .RegRead method, which then extracts the registry values. Finally output the information with an echo command. I say again, next week we will take a few risks and write to the registry, but before we make any changes, let us master the basics of registry scripting.
As ever, there are also pure scripting points that I would like to highlight. Many Windows 2003 VBScripts create a 'Network' object, however, this script creates a different object - 'Shell' rather than ' Network'. I am also on the lookout for verbs that extend our range of VBScript methods, so this week I feature .RegRead and next week it will the turn of .RegWrite. In summary, this week's example centers on: objShell.RegRead
Some registry paths can be very long. So, to control lines of code greater than 80 characters, the script uses the _ (Underscore). Technically, when VBScript sees a carriage return, it thinks there is a new line of code. By employing _, you can effectively join two lines in notepad to be interpreted as a single line of code by the script host (WSH).
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Note 1: In the script I shortened the registry hive: HKEY_LOCAL_MACHINE to HKLM, this is a well know abbreviation, and it works well.
Note 2: Observe how I recycle the strWinLogon for 3 separate values.
Note 3: Study the use of vbCr (Visual Basic Carriage Return). Remember that vbCr is for displaying the message box. vbCr is different from _ (underscore), which was for joining two lines in the script itself.
Note 4: Deploying _ looks easy, but there are traps, observe where to position the accompanying speech marks & (ampersand).
My greatest joy would be if you adapted my script to read other registry values. Extend the script to read not only from the WinLogon section but also from the many other folders in the registry. So, amend my script to read other values, then echo the output to a message box.
This week's hidden agenda is attention to detail. If you are trying this at home, then check each speech mark, underscore and ampersand. What would be wrong with this?
Answer you need two extra speech marks.
Also, the ampersand or to be precise the lack of an ampersand can be annoying. I will let you into a secret, I rarely code a concatenation section without omitting at least one &. For example I made a mistake here:
".... Microsoft\" _
The answer was to add an ampersand on line 2
".... Microsoft\" _
Here is a simple script to read values from the registry. Even though the concept is straightforward, take the time to understand the fine detail of the VBScript command, for example vbCr, _(underscore) and & (ampersand).
See more about registry scripts