Solved

How to find if a Reg key exists

Posted on 2007-11-19
1
434 Views
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, _
        strValueName,strValue
  'If IsNull(strValueName) Then
   if strValueName = "InstalledDate" Then
      objFile2.WriteLine   "Date Installed: " & strValue
         oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath, _
         strValueName1,strValue1
           strValueName1 = "Description"
         objFile2.WriteLine  "Patch Installed:  " & strValue1
        
  Else
      objFile2.WriteLine  "Key (" & strCharacters & ") does not exist!!"  & strValue1      

  End If
Loop
objFile.Close
objFile2.Close
0
Comment
Question by:Lou Pereira
1 Comment
 
LVL 12

Accepted Solution

by:
chandru_sol earned 250 total points
ID: 20314323
Try this..........

regards
Chandru
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

   Else

      CheckIfRegKeyExist = False

   End If

   Set objRegistry = Nothing

End Function

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now