?
Solved

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

Posted on 2011-02-14
12
Medium Priority
?
6,227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 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