Link to home
Start Free TrialLog in
Avatar of Bladey001
Bladey001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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!
Avatar of Rob
Rob
Flag of Australia image

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

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bladey001

ASKER

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

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.
That's a good point made by Rob - you don't want any confirmation boxes popping up.
Avatar of matrixnz
matrixnz

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