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

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
 
snwl-itservicesAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
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
 
RobSampsonCommented:
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
 
snwl-itservicesAuthor Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
RobSampsonCommented:
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
 
snwl-itservicesAuthor Commented:
I tried the VBS above and nothing installs.

Any other ideas? Thanks for the helping me out.
0
 
RobSampsonCommented:
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
 
snwl-itservicesAuthor Commented:
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
 
snwl-itservicesAuthor Commented:
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
 
RobSampsonCommented:
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
 
snwl-itservicesAuthor Commented:
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
 
RobSampsonCommented:
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
 
snwl-itservicesAuthor Commented:
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
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.