Link to home
Start Free TrialLog in
Avatar of Lou Pereira
Lou PereiraFlag for United States of America

asked on

How to find if a Reg key exists

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
ASKER CERTIFIED SOLUTION
Avatar of chandru_sol
chandru_sol
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial