Solved

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

Posted on 2010-09-20
27
609 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:cmcnulty
  • 12
  • 11
  • 3
  • +1
27 Comments
 
LVL 8

Expert Comment

by:spinzr0
ID: 33721962
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
 
LVL 30

Expert Comment

by:Brad Howe
ID: 33721993
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
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33722136
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
 

Author Comment

by:cmcnulty
ID: 33724378
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
 
LVL 8

Expert Comment

by:spinzr0
ID: 33724913
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
 
LVL 8

Expert Comment

by:spinzr0
ID: 33724959
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
 

Author Comment

by:cmcnulty
ID: 33725098
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
 
LVL 8

Expert Comment

by:spinzr0
ID: 33725108
In my code, both keys must exist for it to install.  IF the HKCU optional?
0
 

Author Comment

by:cmcnulty
ID: 33725142
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
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33725323
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
 
LVL 8

Expert Comment

by:spinzr0
ID: 33725409
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
 

Author Comment

by:cmcnulty
ID: 33727205
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
 
LVL 8

Expert Comment

by:spinzr0
ID: 33727222
is the new VB working?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:cmcnulty
ID: 33727382
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
 
LVL 8

Accepted Solution

by:
spinzr0 earned 500 total points
ID: 33727639
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
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33729856
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
 

Author Comment

by:cmcnulty
ID: 33730001
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
 
LVL 8

Expert Comment

by:spinzr0
ID: 33730216
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
 

Author Comment

by:cmcnulty
ID: 33736686
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
 
LVL 8

Expert Comment

by:spinzr0
ID: 33736802
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
 

Author Comment

by:cmcnulty
ID: 33737369
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
 
LVL 8

Expert Comment

by:spinzr0
ID: 33737402
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
 

Author Comment

by:cmcnulty
ID: 33743379
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
 
LVL 8

Expert Comment

by:spinzr0
ID: 33743644
the script can run from the share.  So instead of the URL, it would be \\servername\shared folder\myfile.msi.
0
 

Author Comment

by:cmcnulty
ID: 33753288
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
 

Author Comment

by:cmcnulty
ID: 33753371
Also it appears that I can only access these files with my browser. I tried \\servername\sharefolder\file.msi and I get nothing.
0
 
LVL 8

Expert Comment

by:spinzr0
ID: 33753631
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

13 Experts available now in Live!

Get 1:1 Help Now