PowerShell Ezine, Logon Scripts

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.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


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

ˆ

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 http://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).

ˇ

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 a whole section about the registry here

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


Google

Webcomputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.