Solved

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

Posted on 2011-02-14
12
5,500 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

21 Experts available now in Live!

Get 1:1 Help Now