?
Solved

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

Posted on 2011-02-14
12
Medium Priority
?
6,579 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

807 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