VBScript Registry Check

Posted on 2012-08-16
Last Modified: 2012-09-06
The script is set to look for folders and create them.  Then to check for a registry key path and if it doesn't exist to run an install.  It runs properly the first time through, but subsequent runs of the scripts do not run the MSI, confirmed that the key is gone, and trying to run the script without the registry check launches the MSI.

Any help is appreciated.

Dim objFSO, WshShell

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."
Set wshShell = CreateObject("WScript.Shell")
Set objRegistry = GetObject("winmgmts:\\" & _
     strComputer & "\root\default:StdRegProv")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists("C:\Windows\TEMP\") Then
End If

If Not objFSO.FolderExists("C:\Windows\TEMP\Logs") Then
End If
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
strValueName = "{3FE28AA5-8140-47E4-A0D3-974F24831556}"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName
      If IsNull(strValueName) Then "msiexec.exe /i <path to MSI> /qn /l*v C:\Windows\temp\Logs\GFI2012GPOInstall.log"
End If
Question by:aquias2000
    LVL 10

    Expert Comment

    I forget why, but I remember that checking for the EXISTENCE of a registry key using WMI is tricky.

    Here is the most reliable method I've created:

    Function RegValueExists(byval RegHive, byval RegKey, byval RegValue)
      Dim aValueNames, aValueTypes
      RegValueExists = False   ' init value
      If oReg.EnumValues(RegHive, RegKey, aValueNames, aValueTypes) = 0 Then
        If IsArray(aValueNames) Then
          For i = 0 To UBound(aValueNames)
            If LCase(aValueNames(i)) = LCase(RegValue) Then
              RegValueExists = True
              Exit Function
            End If
        End If
      End If
    End Function

    Open in new window

    LVL 17

    Expert Comment

    by:Jared Luker
    Usually when I'm checking to see if a registry key exists, I'll just use wshshell.regread with 'on error goto next' turned on

    on error goto next
    KeyCheck = wshshell.regread "hklm\Software\Microsoft\Windows\CurrentVersion\Uninstall\{3FE28AA5-8140-47E4-A0D3-974F24831556}
    on error goto 0

    If the key exists err.number will be 0.  If it does not exist, err.number will be <> 0.

    If err.number <> 0 Then "msiexec.exe /i <path to MSI> /qn /l*v C:\Windows\temp\Logs\GFI20

    See if that helps...
    LVL 17

    Accepted Solution

    on error goto next
    KeyCheck = wshshell.regread "hklm\Software\Microsoft\Windows\CurrentVersion\Uninstall\{3FE28AA5-8140-47E4-A0D3-974F24831556}"
    on error goto 0
    If err.number <> 0 Then "msiexec.exe /i <path to MSI> /qn /l*v C:\Windows\temp\Logs\GFI20
    End if

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
    Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now