Solved

VBScript - Check to see if program is installed if not install

Posted on 2011-02-14
12
5,759 Views
Last Modified: 2012-05-11
Hi,

I wrote a script (below) for it to check if the reg/software is install and if its not to install it base on the processor 32 or 64 bits.

I also have this without a version check and it works perfectly, but I would like to check the version before installing.

I keep getting a "Line 15 Char 11 Error Expected end of statement" Any ideas?

BROKEN***************************

Dim strComputer, intAddressWidth, objShell
Set objFso = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")

For Each objProcessor in colProcessors
      intAddressWidth = objProcessor.AddressWidth
Next

Set objShell = Wscript.CreateObject("Wscript.Shell")

Reg Query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{32F23BEE-A63F-448F-A2DA-93E234C2A418}

if %errorlevel%==0 goto end

If Not objFso.FileExists("C:\Program Files\File3") Then
Elseif intAddressWidth = 32 Then
   objShell.Run("\\dfs2\IT\File32.exe")
ElseIf intAddressWidth = 64 Then
   objShell.Run("\\dfs2\IT\File64.exe")
Else
   Wscript.Echo "OS not detected"
End If

:end

*******************************************

Dim strComputer, intAddressWidth, objShell
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")

For Each objProcessor in colProcessors
      intAddressWidth = objProcessor.AddressWidth
Next

Set objShell = Wscript.CreateObject("Wscript.Shell")

If intAddressWidth = 32 Then
   objShell.Run("\\dfs2\IT\File32.exe")
ElseIf intAddressWidth = 64 Then
   objShell.Run("\\dfs2\IT\File64.exe")
Else
   Wscript.Echo "Os not detected."
End If
 
0
Comment
Question by:snwl-itservices
  • 6
  • 6
12 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 34893135
Hi, the reg query you are using is batch code.  Try this version with VBS code.

Regards,

Rob.
Dim strComputer, intAddressWidth, objShell
Set objFso = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")

For Each objProcessor in colProcessors
      intAddressWidth = objProcessor.AddressWidth
Next

Set objShell = Wscript.CreateObject("Wscript.Shell")

On Error Resume Next
intReturn = objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{32F23BEE-A63F-448F-A2DA-93E234C2A418}")
If Not (intReturn = 0 Or Err.Number = 0) Then
	Err.Clear
	On Error GoTo 0
	If Not objFso.FileExists("C:\Program Files\File3") Then
	Elseif intAddressWidth = 32 Then
		objShell.Run("\\dfs2\IT\File32.exe")
	ElseIf intAddressWidth = 64 Then
		objShell.Run("\\dfs2\IT\File64.exe")
	Else
		Wscript.Echo "OS not detected"
	End If
End If
Err.Clear
On Error GoTo 0

Open in new window

0
 

Author Comment

by:snwl-itservices
ID: 34899202
Hi Rob,

I tried the VBS above and does not install the software when the script is ran (does not have the software installed).

Any other ideas?

Thanks
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34903282
Sorry, try this.  I think I had the logic wrong.

Regards,

Rob.
Dim strComputer, intAddressWidth, objShell
Set objFso = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")

For Each objProcessor in colProcessors
      intAddressWidth = objProcessor.AddressWidth
Next

Set objShell = Wscript.CreateObject("Wscript.Shell")

On Error Resume Next
strRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{32F23BEE-A63F-448F-A2DA-93E234C2A418}"
If Right(strRegKey, 1) <> "\" Then strRegKey = strRegKey & "\"
strReturn = objShell.RegRead(strRegKey)
If Err.Number <> 0 Then
	Err.Clear
	On Error GoTo 0
	If Not objFso.FileExists("C:\Program Files\File3") Then
		' Do nothing
	Elseif intAddressWidth = 32 Then
		objShell.Run("\\dfs2\IT\File32.exe")
	ElseIf intAddressWidth = 64 Then
		objShell.Run("\\dfs2\IT\File64.exe")
	Else
		Wscript.Echo "OS not detected"
	End If
End If
Err.Clear
On Error GoTo 0

Open in new window

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:snwl-itservices
ID: 34909380
I tried the VBS above and nothing installs.

Any other ideas? Thanks for the helping me out.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34911752
OK, try this. I haven't changed any functionality, but just added some output statements at each branch, so you can see exactly what's happening.

Regards,

Rob.
Dim strComputer, intAddressWidth, objShell
Set objFso = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")

For Each objProcessor in colProcessors
      intAddressWidth = objProcessor.AddressWidth
Next

Set objShell = Wscript.CreateObject("Wscript.Shell")

On Error Resume Next
strRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{32F23BEE-A63F-448F-A2DA-93E234C2A418}"
If Right(strRegKey, 1) <> "\" Then strRegKey = strRegKey & "\"
strReturn = objShell.RegRead(strRegKey)
If Err.Number <> 0 Then
	Wscript.Echo "There was an error reading" & VbCrLf & strRegKey & VbCrLf & "This means the key could not be found. Checking for install..."
	Err.Clear
	On Error GoTo 0
	If Not objFso.FileExists("C:\Program Files\File3") Then
		Wscript.Echo "The file C:\Program Files\File3 does not exist. Not installing the software."
		' Do nothing
	Elseif intAddressWidth = 32 Then
		Wscript.Echo "Installing File32.exe"
		objShell.Run("\\dfs2\IT\File32.exe")
	ElseIf intAddressWidth = 64 Then
		Wscript.Echo "Installing File64.exe"
		objShell.Run("\\dfs2\IT\File64.exe")
	Else
		Wscript.Echo "OS not detected"
	End If
Else
	Wscript.Echo "The key " & VbCrLf & strRegKey & VbCrLf & "was found.  Not installing the software."
End If
Err.Clear
On Error GoTo 0

Open in new window

0
 

Author Comment

by:snwl-itservices
ID: 34911803
Hi Rob,

I ran the one with the alert and I checked one with the software installed and I the "Reg Key found. Not installing the software"

Then I check one without the software installed and a I this alert

"Wscript.Echo "The file C:\Program Files\File3.exe does not exist. Not installing the software.""

Thanks!
0
 

Author Comment

by:snwl-itservices
ID: 34911820
I changed the

If Not objFso.FileExists("C:\Program Files\File3") Then
            Wscript.Echo "The file C:\Program Files\File3 does not exist. Not installing the software."
            ' Do nothing

To

If objFso.FileExists("C:\Program Files\File3") Then
            Wscript.Echo "The file C:\Program Files\File3 does not exist. Not installing the software."
            ' Do nothing

I think that was the issue.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34911836
Right, it probably was.

This:
If Not objFso.FileExists("C:\Program Files\File3") Then

says "If File3 DOES NOT EXIST", while this:

If objFso.FileExists("C:\Program Files\File3") Then

says "If File3 DOES EXIST".  Personally, I prefer to use

If objFSO.FileExists("C:\Program Files\File3") = True Then
or
If objFSO.FileExists("C:\Program Files\File3") = False Then


depending on what you're after.

Regards,

Rob.
0
 

Author Comment

by:snwl-itservices
ID: 34911890
One more question. Can you help me with adding a 2nd REGEDIT check?

The software installs a 32 and 64 version so the reg are different and both need to be checked.

Thanks alot Rob. I appeciate it!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34911979
OK, see how this goes.

Regards,

Rob.
Dim strComputer, intAddressWidth, objShell
Set objFso = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")

For Each objProcessor in colProcessors
      intAddressWidth = objProcessor.AddressWidth
Next

Set objShell = Wscript.CreateObject("Wscript.Shell")

On Error Resume Next
arrRegKeys = Array( _
	"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{32F23BEE-A63F-448F-A2DA-93E234C2A418}", _
	"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{32F23BEE-A63F-448F-A2DA-93E234C2A418}" _
	)
boolRegFound = False
For Each strRegKey In arrRegKeys
	If Right(strRegKey, 1) <> "\" Then strRegKey = strRegKey & "\"
	strReturn = objShell.RegRead(strRegKey)
	If Err.Number = 0 Then boolRegFound = True
	Err.Clear
Next

If boolRegFound = False Then
	Wscript.Echo "There was an error finding the registry keys." & VbCrLf & "This means the key could not be found. Checking for install..."
	Err.Clear
	On Error GoTo 0
	If objFso.FileExists("C:\Program Files\File3") = True Then
		Wscript.Echo "The file C:\Program Files\File3 already exists. Not installing the software."
		' Do nothing
	Elseif intAddressWidth = 32 Then
		Wscript.Echo "Installing File32.exe"
		objShell.Run("\\dfs2\IT\File32.exe")
	ElseIf intAddressWidth = 64 Then
		Wscript.Echo "Installing File64.exe"
		objShell.Run("\\dfs2\IT\File64.exe")
	Else
		Wscript.Echo "OS not detected"
	End If
Else
	Wscript.Echo "The key " & VbCrLf & strRegKey & VbCrLf & "was found.  Not installing the software."
End If
Err.Clear
On Error GoTo 0

Open in new window

0
 

Author Comment

by:snwl-itservices
ID: 34912234
That worked great.

On the last note is there a way to only have the alert show up when running the script through command prompt versus pop up?

Thanks for all the help!
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34912542
Yes, you can check if the script has been run with cscript, and only display the messages if that's the case.

Regards,

Rob.
Dim strComputer, intAddressWidth, objShell
Set objFso = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")

For Each objProcessor in colProcessors
      intAddressWidth = objProcessor.AddressWidth
Next

Set objShell = Wscript.CreateObject("Wscript.Shell")

On Error Resume Next
arrRegKeys = Array( _
	"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{32F23BEE-A63F-448F-A2DA-93E234C2A418}", _
	"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{32F23BEE-A63F-448F-A2DA-93E234C2A418}" _
	)
boolRegFound = False
For Each strRegKey In arrRegKeys
	If Right(strRegKey, 1) <> "\" Then strRegKey = strRegKey & "\"
	strReturn = objShell.RegRead(strRegKey)
	If Err.Number = 0 Then boolRegFound = True
	Err.Clear
Next

If boolRegFound = False Then
	If LCase(Right(Wscript.FullName, 11)) = "cscript.exe" Then Wscript.Echo "There was an error finding the registry keys." & VbCrLf & "This means the key could not be found. Checking for install..."
	Err.Clear
	On Error GoTo 0
	If objFso.FileExists("C:\Program Files\File3") = True Then
		If LCase(Right(Wscript.FullName, 11)) = "cscript.exe" Then Wscript.Echo "The file C:\Program Files\File3 already exists. Not installing the software."
		' Do nothing
	Elseif intAddressWidth = 32 Then
		If LCase(Right(Wscript.FullName, 11)) = "cscript.exe" Then Wscript.Echo "Installing File32.exe"
		objShell.Run("\\dfs2\IT\File32.exe")
	ElseIf intAddressWidth = 64 Then
		If LCase(Right(Wscript.FullName, 11)) = "cscript.exe" Then Wscript.Echo "Installing File64.exe"
		objShell.Run("\\dfs2\IT\File64.exe")
	Else
		If LCase(Right(Wscript.FullName, 11)) = "cscript.exe" Then Wscript.Echo "OS not detected"
	End If
Else
	If LCase(Right(Wscript.FullName, 11)) = "cscript.exe" Then Wscript.Echo "The key " & VbCrLf & strRegKey & VbCrLf & "was found.  Not installing the software."
End If
Err.Clear
On Error GoTo 0

Open in new window

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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