How to find if a Reg key exists

Posted on 2007-11-19
Medium Priority
Last Modified: 2008-02-01
I'm trying to create a script that will the registry (HKLM\Software\Microsoft\Updates\Windows Server 2003\SP3\ "keynamevar").   If the key exists then create a report text file.  I've created the following, but when I test it does not report true findings!  Thank you for your help

strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const HKEY_LOCAL_MACHINE = &H80000002

dt = Replace(Date,"/","-")
strFileName = "C:\versions\" & dt & "-FreeSpace.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO.CreateTextFile(strFileName, ForAppending, True)
Set objFile = objFSO.OpenTextFile("C:\versions\patches.txt", 1)

' Registry values
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")
'objFile2.WriteLine "Date Modified: " & dt
Do Until objFile.AtEndOfStream
    strCharacters = objFile.Readline
    strKeyPath = "SOFTWARE\Microsoft\Updates\Windows Server 2003\SP3\"& strCharacters
    strValueName = "InstalledDate"
    oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath, _
  'If IsNull(strValueName) Then
   if strValueName = "InstalledDate" Then
      objFile2.WriteLine   "Date Installed: " & strValue
         oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath, _
           strValueName1 = "Description"
         objFile2.WriteLine  "Patch Installed:  " & strValue1
      objFile2.WriteLine  "Key (" & strCharacters & ") does not exist!!"  & strValue1      

  End If
Question by:Lou Pereira
1 Comment
LVL 12

Accepted Solution

chandru_sol earned 1000 total points
ID: 20314323
Try this..........

Option Explicit
Dim blnResult
blnResult = CheckIfRegKeyExist()
If blnResult Then WScript.Echo "True" Else WScript.Echo "False"
Function CheckIfRegKeyExist()
   Const HKEY_LOCAL_MACHINE = &H80000002
   Dim objRegistry
   Dim arrSubKeys
   Dim strStartKey
   Dim varResult
   ' Set a default value of strStartKey as example.
   ' User can type any other key.
   strStartKey = "SOFTWARE\Microsoft\"
   strStartKey = InputBox("Which key in HKLM do you want to enumerate?","Registry Key Enumeration", strStartKey)
   ' Connect to the registry.
   Set objRegistry = GetObject("winmgmts:root\default:stdregprov")
   If objRegistry.EnumKey(HKEY_LOCAL_MACHINE, strStartKey, arrSubKeys) = 0 Then
      CheckIfRegKeyExist = True
      CheckIfRegKeyExist = False
   End If
   Set objRegistry = Nothing
End Function

Open in new window


Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question