Guy’s Scripting Ezine 62 – Registry

Contents for Ezine 62 – Registry

 ♣

This Week’s Secret

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.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

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 onUMRA.

Mission to Read the Registry

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) IP Tracker

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

Example – To Extract Logon Information from the Registry

Instructions

  1. Copy and paste the script below into notepad. Alternatively, get a trial copy of OnScript.
  2. Save the file with .vbs extension e.g. ReadReg.vbs
  3. Double click and then check the message box
  4. You may like to run Regedit and navigate from the HKLM to the winlogon section
 

‘ ReadReg.vbs
‘ VBScript to read the registry.
‘ Author Guy Thomas https://computerperformance.co.uk
‘ Version 1.4 – March 2006
‘ —————————————-‘

Option Explicit
Dim objShell
Dim strCachedLogon, strShell, strPwdWarn, strWinLogon

strPwdWarn = "passwordexpirywarning"
strShell = "Shell"
strCachedLogon ="cachedlogonscount"

strWinLogon = "HKLM\SOFTWARE\Microsoft\"_
& "Windows NT\currentVersion\Winlogon\"

Set objShell = CreateObject("WScript.Shell")

strShell = objShell.RegRead(strWinLogon & strShell)
strPwdWarn = objShell.RegRead(strWinLogon & strPwdWarn)
strCachedLogon = objShell.RegRead(strWinLogon & strCachedLogon)

Wscript.Echo "Shell Program " & vbTab & strShell & vbCr _
& "Password Expires " & vbTab & strPwdWarn & vbCr _
& "Cached Logons " & vbTab & strCachedLogon

WScript.Quit

‘ End of Registry Example VBscript.

Learning Points

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).

See more Win 8 registry examples.

Challenges

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?

"…. Microsoft\ _
& Windows NT\……"

Answer you need two extra speech marks.

"…. Microsoft\"_
& "Windows NT\……"

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\" _
 "Windows NT\……"

The answer was to add an ampersand on line 2

"…. Microsoft\" _
& "Windows NT\……"

Summary – Reading the Registry

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

Registry  • Win Registry Hacks  •PowerShell Registry  • Ezine 8 Registry  • Ezines  •Tool Kit

Ezine 62 Registry  •Ezine 63 Registry  • Ezine 64 Write Registry  • Ezine 119 Registry shortcuts 

Ezine 121 CachedLogons  • Ezine 136 remove shortcut  •Ezine 137 Remove shortcut