I am trying to write a vb script (or batch file if possible) to perform an installation of an .msi file if a particular registry key in HKCR does not exist

I am trying to write a vb script (or batch file if possible) to perform an installation of an .msi file if a particular registry key in HKCR does not exist.  If the registry key does exist, I do not want the install to take place.
cmcnultyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
spinzr0Connect With a Mentor Commented:
This should do it.
Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKEY_CLASSES_ROOT\Installer\Products\531837476D83DBA42AFB8A7644796170\") AND RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{74738135-38D6-4ABD-A2BF-A86744971607}\") Then _
    oWShell.Run "msiexec /i ""C:\Temp\PKI-BLADE-ESP-Desktop-09152010-(7.2.3)\MiddlewareAndCardReaderDrivers\SafeNet-BeSec-Middleware-723.msi"" /passive /norestart"

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function

Open in new window

0
 
spinzr0Commented:
Here you go.  Just change HKCU\Software\Microsoft\Office\Version to be your reg key and c:\my folder\mymsi.msi to be the full path to your MSI.
Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKCU\Software\Microsoft\Office\Version") Then _
    oWShell.Run "msiexec /i ""c:\my folder\mymsi.msi"" /qn

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function

Open in new window

0
 
Brad HoweDevOps ManagerCommented:
Merge the following solutions
Check Registry
http://www.experts-exchange.com/Programming/Misc/Q_21844234.html 
If key found Install silient
http://www.experts-exchange.com/Programming/Installation/MSI/Q_24840781.html 
For the registry constants
     Const HKLM = &H80000002
     Const HKCR= &H80000000T
     Const HKCU = &H80000001
     Const HKUSERS = &H800000
Let us know if you need help.
Cheers, Hades666
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
this is very easy in a batch script, it can all be done in a single line, however since I don;t know what key you want and I don't know that the path to your MSI file is let alone what it's name is, I'm it a few more lines to be a bit easier for you to edit. ^^


@ECHO OFF

::Set Variables Here
SET "KeyToFind=HKCR\Test key\here"
SET "FileToInstall=\\Server\Share\Folder\File.msi"

::- This Actually checks the key and calls the MSI, but ONLY if the key is NOT found.
REG QUERY "%KeyToFind%" 2>&1 | FIND /I "ERROR: The system was unable to find the specified registry key or value." > nul && "%FileToInstall%"

Open in new window

0
 
cmcnultyAuthor Commented:
thanks for the help.  I just realized that I have to do a search in 2 places in the registry. If hkcr/ does not exist and hklm/ does exist then perform .msi install.  How can I add the second check in the registry for hklm/ to the vb script?
0
 
spinzr0Commented:
Here you go.
Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKCU\Software\Microsoft\Office\Version") AND Not RegKeyExists("HKLM\Software\Microsoft\Office\Version") Then _
    oWShell.Run "msiexec /i ""c:\my folder\mymsi.msi"" /qn

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function

Open in new window

0
 
spinzr0Commented:
As a note this installs the MSI silently.  Do you want to be able to see the progress of the MSI installing?  If so, let me know and I can modify.
0
 
cmcnultyAuthor Commented:
Thanks spinzr0.  It appears that your script might be checking if both registry keys do not exist then performing install.  I just wanted to reitierate that the second reg key check must exist.  I will be adding the /passive /norestart switch in lue of the /qb.  Thanks
0
 
spinzr0Commented:
In my code, both keys must exist for it to install.  IF the HKCU optional?
0
 
cmcnultyAuthor Commented:
If HKCR\ does not exist and if HKLM\ does exist then perform install.  I do not want the install to run If they both exist and/or if none exist.  Thanks.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
here is the batch script modified so it will only install the MSI file if the HKCR Key DOES NOT EXIST and the HKLM Key DOES Exist

@ECHO OFF

::Set Variables Here
SET "KeyToFindHKCR=HKCR\Test key\here"
SET "KeyToFindHKLM=HKLM\Test key\here"
SET "FileToInstall=\\Server\Share\Folder\File.msi"

::- This Actually checks the key and calls the MSI, but ONLY if the HKCR key is NOT found AND the HKLM Key IS Found.
REG QUERY "%KeyToFindHKCR%" 2>&1 | FIND /I "ERROR: The system was unable to find the specified registry key or value." > nul && REG QUERY "%KeyToFindHKLM%" 2>&1 | FIND /I "ERROR: The system was unable to find the specified registry key or value." > nul || "%FileToInstall%"

Open in new window

0
 
spinzr0Commented:
here you go
Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKCU\Software\Microsoft\Office\Version") AND RegKeyExists("HKLM\Software\Microsoft\Office\Version") Then _
    oWShell.Run "msiexec /i ""c:\my folder\mymsi.msi"" /qn

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function

Open in new window

0
 
cmcnultyAuthor Commented:
Thanks QCubed.  I tried your batch but it appears that it will perform the install no matter what.  Ive tested this by changing the HKLM key to a known non-existent key.  What am I doing wrong?  Here is my code:

@ECHO OFF

::Set Variables Here
SET "KeyToFindHKCR=HKCR\Installer\Products\531837476D83DBA42AFB8A7644796170"
SET "KeyToFindHKLM=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{74738135-38D6-4ABD-A2BF-A86744971607}"
SET "FileToInstall=C:\Temp\PKI-BLADE-ESP-Desktop-09152010-(7.2.3)\MiddlewareAndCardReaderDrivers\SafeNet-BeSec-Middleware-723.msi"

::- This Actually checks the key and calls the MSI, but ONLY if the HKCR key is NOT found AND the HKLM Key IS Found.
REG QUERY "%KeyToFindHKCR%" 2>&1 | FIND /I "ERROR: The system was unable to find the specified registry key or value." > nul && REG QUERY "%KeyToFindHKLM%" 2>&1 | FIND /I "ERROR: The system was unable to find the specified registry key or value." > nul || "%FileToInstall%"
0
 
spinzr0Commented:
is the new VB working?
0
 
cmcnultyAuthor Commented:
I tried the new vb and I am getting an expected end of staement error that occurs on line 4, char 30.  here is my code:

Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKCR\Installer\Products\531837476D83DBA42AFB8A7644796170") AND RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{74738135-38D6-4ABD-A2BF-A86744971607}") Then _
    oWShell.Run "msiexec /i" "C:\Temp\PKI-BLADE-ESP-Desktop-09152010-(7.2.3)\MiddlewareAndCardReaderDrivers\SafeNet-BeSec-Middleware-723.msi" /passive /norestart

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function
 
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
I assume you are running this on at least a windows XP machine correct?  Works fine on Windows 7 as-is but here it is a bit more verbose.  Please see attached.

:: Script Name: CheckRegToRunCmd.bat
@ECHO OFF

::Set Variables Here
SET "StatusHKCR=GOOD"
SET "StatusHKLM=BAD"
SET "KeyToFindHKCR=HKCR\Installer\Products\531837476D83DBA42AFB8A7644796170"
SET "KeyToFindHKLM=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{74738135-38D6-4ABD-A2BF-A86744971607}"
SET "FileToInstall=\\Server\Share\Folder\File.msi"

::- Here we Actually check the key and call the MSI; but ONLY if the HKCR key is NOT found, AND the HKLM key IS found.
REG QUERY "%KeyToFindHKCR%" 2>&1 | FIND /I "ERROR: The system was unable to find the specified registry key or value." > nul && SET "StatusHKCR=BAD"
REG QUERY "%KeyToFindHKLM%" 2>&1 | FIND /I "ERROR: The system was unable to find the specified registry key or value." > nul || SET "StatusHKLM=GOOD"

IF /I "%StatusHKCR%%StatusHKLM%"=="BADGOOD" ECHO Running "%FileToInstall%"...&"%FileToInstall%"

Open in new window

0
 
cmcnultyAuthor Commented:
Ok thanks.  I actually am trying to run the uninstall of the .msi after the install completes.  Is there a way state that if the install runs, then after it completes, uninstall the program.
0
 
spinzr0Commented:
Here you go.
Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKEY_CLASSES_ROOT\Installer\Products\531837476D83DBA42AFB8A7644796170\") AND RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{74738135-38D6-4ABD-A2BF-A86744971607}\") Then
    'Install the product 
    oWShell.Run "msiexec /i ""C:\Temp\PKI-BLADE-ESP-Desktop-09152010-(7.2.3)\MiddlewareAndCardReaderDrivers\SafeNet-BeSec-Middleware-723.msi"" /passive /norestart"
    'Now remove it
    oWShell.Run "msiexec /x ""C:\Temp\PKI-BLADE-ESP-Desktop-09152010-(7.2.3)\MiddlewareAndCardReaderDrivers\SafeNet-BeSec-Middleware-723.msi"" /passive /norestart"
End If

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function

Open in new window

0
 
cmcnultyAuthor Commented:
Thanks.  The vb script installs the program nicely but doesnt uninstall once the install completes.  Is there a command to tell it to wait until the installation completes before continuing with the uninstall command?
0
 
spinzr0Commented:
Here you go.  It will not wait for it to install, wait 5 seconds (to ensure everything is done), then remove.
Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKEY_CLASSES_ROOT\Installer\Products\531837476D83DBA42AFB8A7644796170\") AND RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{74738135-38D6-4ABD-A2BF-A86744971607}\") Then
    'Install the product 
    oWShell.Run "msiexec /i ""C:\Temp\PKI-BLADE-ESP-Desktop-09152010-(7.2.3)\MiddlewareAndCardReaderDrivers\SafeNet-BeSec-Middleware-723.msi"" /passive /norestart", 0, True
    'Wait 5 seconds
    Wscript.Sleep 5000
    'Now remove it
    oWShell.Run "msiexec /x ""C:\Temp\PKI-BLADE-ESP-Desktop-09152010-(7.2.3)\MiddlewareAndCardReaderDrivers\SafeNet-BeSec-Middleware-723.msi"" /passive /norestart", 0, True
End If

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function

Open in new window

0
 
cmcnultyAuthor Commented:
Thank you for all your help.  It has been very educational.  Now instead of installing, I am looking to add 1 more search for an additional reg key and instead of installing I am looking to run in a .reg file.  here is the code that I have but something is wrong.  Can you correct it? Thanks

Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKEY_CLASSES_ROOT\Installer\Products\5D6695773F52E004F94E799862197B3C\") AND Not RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Installer\Products\5D6695773F52E004F94E799862197B3C\") AND RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{775966D5-25F3-400E-9FE4-97892691B7C3}\") Then
    'Install the product
    oWShell.Run regedit /S "C:\Temp\PKI-BLADE-ESP-Desktop-09152010\BatchFiles\safenet612hkcr.reg"
    Wscript.Sleep 5000
    'Now remove it
    oWShell.Run "msiexec /x ""C:\Temp\PKI-BLADE-ESP-Desktop-09152010\MiddlewareAndCardReaderDrivers\SafeNet-Borderless-Security-PK-Client.msi"" /passive /norestart"
End If

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function
0
 
spinzr0Commented:
you're close.  Its just the order of the quotes.
Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKEY_CLASSES_ROOT\Installer\Products\5D6695773F52E004F94E799862197B3C\") AND Not RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Installer\Products\5D6695773F52E004F94E799862197B3C\") AND RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{775966D5-25F3-400E-9FE4-97892691B7C3}\") Then
    'Install the product 
    oWShell.Run "regedit /S ""C:\Temp\PKI-BLADE-ESP-Desktop-09152010\BatchFiles\safenet612hkcr.reg"""
    Wscript.Sleep 5000
    'Now remove it
    oWShell.Run "msiexec /x ""C:\Temp\PKI-BLADE-ESP-Desktop-09152010\MiddlewareAndCardReaderDrivers\SafeNet-Borderless-Security-PK-Client.msi"" /passive /norestart"
End If

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function

Open in new window

0
 
cmcnultyAuthor Commented:
Thanks spinzr0!  Everything works like a charm now.  I was wondering, is there a way to have the .reg run (or .msi (un)install) from an intranet site?  For example:

Set oWShell = CreateObject("Wscript.Shell")

If Not RegKeyExists("HKEY_CLASSES_ROOT\Installer\Products\5D6695773F52E004F94E799862197B3C\") AND Not RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Installer\Products\5D6695773F52E004F94E799862197B3C\") AND RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{775966D5-25F3-400E-9FE4-97892691B7C3}\") Then
    'Install the product
    oWShell.Run "regedit /S ""http://mydomainname.com/mydirectory/myfile.reg"""
    Wscript.Sleep 5000
    'Now remove it
    oWShell.Run "msiexec /x ""http://mydomainname.com/mydirectory/myfile.msi"" /passive /norestart"
End If

'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function
0
 
spinzr0Commented:
the script can run from the share.  So instead of the URL, it would be \\servername\shared folder\myfile.msi.
0
 
cmcnultyAuthor Commented:
The problem that I have with this is that these files are copied to three different servers for redundancy in case one goes down.  It would be ideal if I could link everything to the actual web server URL instead.  Are you saying that there is no way to do this?
0
 
cmcnultyAuthor Commented:
Also it appears that I can only access these files with my browser. I tried \\servername\sharefolder\file.msi and I get nothing.
0
 
spinzr0Commented:
From googling around, I see that you can apparently download the file from an HTTP, then save it but I haven't done this before.  Here is a link for it: http://www.suite101.com/content/how-to-use-vbscript-to-download-a-web-page-a89661.

Generally, everything is posted on a share and you just use it as the \\ example above.  To be clear, you have to modify \\servername\sharefolder\file.msi to match your actual server path.  so if the server name is MY-SERVER and the shared folder is MyAppFolder you would use it as \\MY-SERVER\MyAppFolder (the full path to the actual file)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.