batch script help - uninstalling and installing program(s)

Dear experts,

I'm not much of a script writer and need some help!

I need a script e.g. batch script to run on startup to do the following:

1.

Uninstall 3 programs, I have the msiexec code numbers so this is pretty straightforward using msiexec /x {number}

2.

Trickier bit. Check to see if program "Application X" is installed. If it is go to 'end' If not install from \\server\applicationx.msi
BTW 'Application X' installs to a different path depending on whether the local machine is WinXP or Win 7. So ideally if there's just a way to see if the program is installed at all rather than looking to see whether a file exists in C:\ somewhere.

Hope this makes sense!
Bladey001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
Do you need an answer to Part 1?  sounds like you know how to do this?  I would use vbscript:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run("msiexec /x {number}")
objShell.Run("msiexec /x {number}")
objShell.Run("msiexec /x {number}")

Open in new window


Use WMI and vb script for enumerating installed programs.  This will help find what's installed on a computer:
http://technet.microsoft.com/en-us/library/ee156540.aspx

I would try using IdentifyingNumber, SKUNumber before Description or Name due to matching strings is a lot harder than numbers.  You would obviously need to know these attributes of each product and instead of outputting to a file as described below, you would test if it matches the description of the products you are wanting to have installed.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("c:\scripts\software.tsv", True)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Product")
objTextFile.WriteLine "Caption" & vbtab & _
"Description" & vbtab & "Identifying Number" & vbtab & _
"Install Date" & vbtab & "Install Location" & vbtab & _
"Install State" & vbtab & "Name" & vbtab & _
"Package Cache" & vbtab & "SKU Number" & vbtab & "Vendor" & vbtab _
 & "Version"
For Each objSoftware in colSoftware
 objTextFile.WriteLine objSoftware.Caption & vbtab & _
 objSoftware.Description & vbtab & _
 objSoftware.IdentifyingNumber & vbtab & _
 objSoftware.InstallLocation & vbtab & _
 objSoftware.InstallState & vbtab & _
 objSoftware.Name & vbtab & _
 objSoftware.PackageCache & vbtab & _
 objSoftware.SKUNumber & vbtab & _
 objSoftware.Vendor & vbtab & _
 objSoftware.Version
Next
objTextFile.Close

Open in new window

0
RobSampsonCommented:
Hi, for VBS code, this should be pretty close to what you need.

Regards,

Rob.

Set objShell = CreateObject("WScript.Shell")
objShell.Run "msiexec /x {GUID1}", 1, True
objShell.Run "msiexec /x {GUID2}", 1, True
objShell.Run "msiexec /x {GUID3}", 1, True

If IsProgramInstalled("Application Display Name") = True Then
	WScript.Echo "Program is already installed"
Else
	objShell.Run "msiexec /i ""\\server\share\application.msi""", 1, True
End If

Function IsProgramInstalled(strSoftware)
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CimV2") 
	Set colSoftware = objWMIService.ExecQuery ("Select * From Win32_Product Where Name ='" & strSoftware & "'")
	blnInstalled = False
	For Each objProduct In colSoftware
		blnInstalled = True
		Exit For
	Next
	IsProgramInstalled = blnInstalled
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobOwner (Aidellio)Commented:
This was what I was getting at (untested):

Set objFSO = CreateObject("Scripting.FileSystemObject")

strComputer = "."

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

Set colSoftware = objWMIService.ExecQuery("SELECT * FROM Win32_Product")


objTextFile.WriteLine "Caption" & vbtab & _
"Description" & vbtab & "Identifying Number" & vbtab & _
"Install Date" & vbtab & "Install Location" & vbtab & _
"Install State" & vbtab & "Name" & vbtab & _
"Package Cache" & vbtab & "SKU Number" & vbtab & "Vendor" & vbtab _
 & "Version"

MySoftware ' dictionary object
' this is an array of software numbers you want to have installed
' I would have the IdentifyingNumber as the index and the execution path to the installer as the value

For Each software in MySoftware
	For Each objSoftware in colSoftware
	    bFound = False
	
		if (objSoftware.IdentifyingNumber = software.IDNumber) Then
			bFound = True
			break
			'objSoftware.Description & vbtab & _
			'objSoftware.IdentifyingNumber & vbtab & _
			'objSoftware.InstallLocation & vbtab & _
			'objSoftware.InstallState & vbtab & _
			'objSoftware.Name & vbtab & _
			'objSoftware.PackageCache & vbtab & _
			'objSoftware.SKUNumber & vbtab & _
			'objSoftware.Vendor & vbtab & _
			'objSoftware.Version
		End If
	Next
	If (bFound) Then
		' no need to install as it is already installed
	Else
		' not found in list of installed software so install it
		Set objShell = CreateObject("Wscript.Shell")
		objShell.Run("msiexec /x " & software.IDNumber)
		Set objShell = Nothing
	End If
Next

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Bladey001Author Commented:
Thanks guys i'll test these out but, I need to add a small step in between 1 & 2. Can you include something that will delete some specific files e.g.
DEL "C:\Program Files\file.txt"
DEL "C:\Program Files\file2.txt"

Sorry to be a pain!
Thanks!
0
RobOwner (Aidellio)Commented:
You can use vbscript for that as well:

Sub DeleteAFile(filespec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   fso.DeleteFile(filespec)
End Sub

Open in new window

0
RobSampsonCommented:
So to use tagit's function, add it to the bottom of the code of your choice (VBS that is) and then at the point you want to delete files, add
DeleteAFile "C:\Files\MyFile.txt"

Open in new window


Although I would also change
fso.DeleteFile(filespec)

Open in new window


To
fso.DeleteFile filespec, True

Open in new window


So that it forces the file deletion.

Rob.
0
RobOwner (Aidellio)Commented:
That's a good point made by Rob - you don't want any confirmation boxes popping up.
0
matrixnzCommented:
Or you could compile the following AutoIT Script (Not Tested) as an executable

#NoTrayIcon

Global $REG_GUIDS[5]
  $REG_GUIDS[0] = 3
  $REG_GUIDS[1] = '{Guid1}'
  $REG_GUIDS[2] = '{Guid2}'
  $REG_GUIDS[3] = '{Guid3}'
  $REG_GUIDS[4] = '{New Application Guid}'

Global $REG_UNINSTALL = 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'

;Uninstalls if the programs exist
For $x = 1 To $REG_GUIDS[0]
  If RegRead($REG_UNINSTALL & '\' & $REG_GUIDS[$x], 'DisplayName') Then RunWait('MSIExec.exe /x' & $REG_GUIDS[$x] & ' /QN /NORESTART')
Next

If FileExists(@ProgramFilesDir & '\file1.txt') Then FileDelete(@ProgramFilesDir & '\file1.txt')
If FileExists(@ProgramFilesDir & '\file2.txt') Then FileDelete(@ProgramFilesDir & '\file2.txt')

If Not RegRead($REG_UNINSTALL & '\' & $REG_GUIDS[4], 'DisplayName') = 'New Application Name' Then
  RunWait('Msiexec.exe /i "' & @ScriptDir & '\MSI Path.msi" /QN /NORESTART')
EndIf

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

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.