user2847663
user2847663

Reputation: 11

VBScript: Error with regex matching incorrect registry values

I am trying to match a Java version in HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall by iterating over the subkeys within Uninstall. I am trying to match a regular expression to Java 7 Update 40, but the regex is matching all DisplayName entries. Below is the code:

 On Error Resume Next 
 Const HKEY_LOCAL_MACHINE = &H80000002 
 Dim oReg 
 Dim objShell

 Set objShell = WScript.CreateObject("WScript.Shell") 
 Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\.\root\default:StdRegProv") 
 Dim sPath, aSub, sKey 
 Set objRegEx = New RegExp 
 objRegEx.Pattern = "\w{4}\s\d{1}\s\w{6}\s\d+" 
 objRedEx.IgnoreCase = True 
 objRegEx.Global = False


 sPath = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" 
 oReg.EnumKey HKEY_LOCAL_MACHINE, sPath, aSub 
 For Each sKey In aSub 
 disName = "HKLM" & "\" & sPath & "\" & sKey & "\DisplayName" 
 unString = "HKLM" & "\" & sPath & "\" & sKey & "\UninstallString" 
 reDisName = objShell.RegRead(disName) 
 reUnString = objShell.RegRead(unString) 
 'Wscript.echo(reDisName)

 If objRexEx.Test( reDisName ) Then 
      Wscript.echo "Match" 
 End If

 'Wscript.echo ObjShell.RegRead(disName) 
 'Wscript.echo ObjShell.RegRead(unString) 
 Next

Sorry if the formatting is off, I put a ctrl-k in front of each code line. This is my first time posting here so go easy...

Upvotes: 1

Views: 623

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

You should start all your scripts with Option Explicit and Dim all your variables. Then you wouldn't need sln's eagle eyes to spot your typo:

Option Explicit
Dim objRegEx : Set objRegEx = New RegExp
objRegEx.Pattern = "\w{4}\s\d{1}\s\w{6}\s\d+"
objRedEx.IgnoreCase = True

output:

cscript 19188400.vbs
...\19188400.vbs(4, 1) Microsoft VBScript runtime error: Variable is undefined: 'objRedEx'

If you insist on using a global On Error Resume Next (a most dangerous mal-practice) then you should disable it until your script is thoroughly debugged. Keeping the OERN in a script known to have even the slightest problem is inviting desaster. Asking for help with code containing a global OERN is futile. So run you program without the OERN and see if the cause for its misbehaviour in't obvious.

Diagnostic output should be as specific as possible. Your WScript.Echo "Match" just shows that the statement is executed; a WScript.Echo "Match", disname would be a bit better. Using .Execute and looking at the Match's details could be more revealing.

The .Pattern should be more specific to. If you look for java updates, anchoring a literal "java" at the start of the string, and asking for "upgrade" instead of "\w{6}" may help to avoid false positives. OTOH, my display names don't look like

Java 7 Update 19

but like

Java(TM) 6 Update 19

and who knows what the next owner of Java will put into the display name.

Upvotes: 1

user557597
user557597

Reputation:

You seem to have a few typo's

objRedEx.IgnoreCase = True
...
If objRexEx.Test( reDisName ) Then

Upvotes: 0

Related Questions