Scripting Question

Need help on how to check if software is latest version, if not uninstall and install latest version.

10/25/2017 3002 views
So I've been searching for a few days now and have come up with a solution that runs outside of my company's K1000 without error. But I'm trying to have a managed installation in place that will check the version of a specific software and if it doesn't match the current version it will uninstall and install the latest MSI. I'm attempting to do this with a zip file that includes a batch file and the MSI for the new installation. The goal with this is to have an easily updatable script for newer versions of this software as the vendor releases new version frequently. When I run it through KACE currently on my test pc (Windows 7) I get zero results. No logs or anything the install just says working and then skips straight to complete with no real results. 

The Contents of my Batch File:

@ECHO off
SET LOGFILE=C:\Temp\Mimcast_install.log
call :logit >> %LOGFILE%
exit /b 0

TASKKILL /IM Outlook.exe
for /f %%a in ('wmic product where 'name like "%%Mimecast for Outlook 32-bit%%"' get version /value^|find /i "Version"') do set "%%~a"
if defined Version (echo %Version%)
if not defined Version (msiexec.exe /quiet /qn /norestart /i "Mimecast for Outlook 7.3.2061.19940 (32 bit).msi")
if "%Version%"=="7.3.2061.19940" (echo Mimecast Current Version Installed) 
echo Not Current Version Installed! Uninstalling Old Version and Installing 7.3.2061.19940!
wmic product where name="Mimecast for Outlook 32-bit" call uninstall
msiexec.exe /quiet /qn /norestart /i "Mimecast for Outlook 7.3.2061.19940 (32 bit).msi" 

Managed Installation Setup:

Answer Summary:
Batch File to uninstall old versions via GUID and install via msiexec at the end is the best method I've found. Props to Chucksteel for pointing it out. Notes: Make sure your .zip archive is not creating a folder on extraction in C:\ProgramData\Dell\KACE\Downloads\{MI FOLDER}
0 Comments   [ + ] Show comments


Answer Chosen by the Author

We have handled uninstalls by calling the msiexec /qn /x {GUID} for all previous versions. If that version doesn't exist on the system then the command fails silently and moves onto the next version. The installation batch file then becomes the series of uninstall commands followed by the install command. When a new version is released, just add another uninstall line and update the install line.

As for the MI not creating the log file are the contents of the zip file being expanded correctly? Also, when you say "the install just says working" what do you mean? Does the KDeploy process run? Does your batch file show up in the process list?
Answered 10/26/2017 by: chucksteel
Red Belt

  • Yes, the KDeploy process runs but the batch file never shows up in the process list and it's almost as if the .zip file including the msi and batch files are never actually transferred to the remote computer. Sorry if I was vague in my explanation of that my mind was in a bit of a fog when i made this..

    I like your idea alot about using the GUID for uninstalling older version. Such a simple solution.. I think i was severly over thinking this. Would you know of a good way to check if the application is installed at all first? I was thinking about writing a redirection statement that checks if the program exists then uninstall via GUID like you suggested and if not just install it outright. I'm relatively new to batch but I'm fluent in several other languages so it's (slowly) coming to me.. I appreciate your response though.
    • Did the zip file get downloaded to C:\ProgramData\Dell\KACE\downloads? There should be a folder named for the ID of the software title (not the ID of the managed install, the associated software title). Check for that folder and its contents.

      You can check for the program by looking for one of the files that it installs first, but in my opinion that just adds another layer of complexity.
      • I'll run some tests now and let you know. I agree that does add complexity just trying to shave off some run time if possible but I'm just going to focus on getting it working correctly then maybe add that in later.
      • After rewriting the batch file and uploading the new .zip file to K1000 I tested and it is downloading and extracting the files to the download folder but i'm still not seeing the bat file run in the processes but do see the KDeploy process.

        Here is my updated batch file:

        @ECHO off
        ::Mimecast for Outlook Install Batch file
        SET LOGFILE=C:\PrgramData\dell\KACE\Mimcast_install.log
        call :logit >> %LOGFILE%
        exit /b 0

        ::Close Outlook
        TASKKILL /IM Outlook.exe

        ::Uninstall earlier Versions

        :: 32 Bit

        MsiExec.exe /qn /X{008134CC-B4AD-47AB-8D4A-E1626749547B}

        MsiExec.exe /qn /X{D8885878-E4DC-4D84-A5D0-682BEB34F208}

        MsiExec.exe /qn /X{3204F788-6F8D-44B1-80E7-DC459A67DD52}

        MsiExec.exe /qn /X{5EF4F25A-BB0D-4C98-987C-BE5B2CD7B190}

        MsiExec.exe /qn /X{A5C7145D-8E1B-44CD-99A3-AC242628C115}

        MsiExec.exe /qn /X{9EC219FE-A5E1-4E18-B975-C84207ECF383}

        MsiExec.exe /qn /X{39208598-361E-47D7-AC86-7800C956BD23}

        :: 64 Bit

        MsiExec.exe /qn /X{E732103C-060A-46F0-BF27-B1E3490423D5}

        MsiExec.exe /qn /X{EE6A70FD-714D-4A05-ABD8-818053504351}

        MsiExec.exe /qn /X{19077201-CBDB-434A-A151-85C62248A471}

        MsiExec.exe /qn /X{6D9219BB-9A0A-4172-8D7D-02B0A77ECF9A}

        ::Install Latest Version

        msiexec.exe /i "Mimecast for Outlook 7.3.2061.19940 (32 bit).msi" /qn /norestart

        I think this is what you were getting at in your first response. Let me know if I'm missing the mark. Again I'm relatively new to writing bat files... I wish I could use powershell :(
    • Are the extracted files at the root of the created folder? Depending on how you created the zip archive they may be inside of another folder and that causes problems.

      I would simplify your batch file further and not use the call command, just in case. You should also be able to specify your command line in the MI as Mimecast_Install.bat > c:\ProgramData\Dell\KACE\mimecast_install.log and that should output everything to that file.
      • They are indeed being extracted into a folder named after the .zip archive. Thanks for that advice on logging I didn't know you could do that. I'll add that into the MI now. As for the .zip archive creating a folder let me see if I can fix that and reupload to test.
      • That did it! The log file was created but nothing was logged. I'll tinker with that some more while testing but this is perfect! I can't thank you enough for your assistance with this I would have spent days trying to figure that out before i realized it was the folder extraction that was causing the issue.

Community Chosen Answer

I would never rely on version data returned by an MSI, purely because vendors are lazy about that kind of thing. A current project has 2 old-version MSIs and 1 current MSI all delivering the exact same version!

It'd be much better to check either the ProductCode or for non-MSI installations, find an executable file or DLL which you know gets updated and get that file's version information. If you want to switch to more robust scripting, the '.GetDetailsOf' method of the FileSystemObject object returns file properties (38 of them!) including version information. Sticking with WMIC in batch, you can use 'datafile', 'get' and 'version'.
Answered 10/26/2017 by: VBScab
Red Belt

  • I think this was the exact issue I was having with my initial script. It was giving me all sorts of weird results trying to match Version types. I'll have to look into the '.GetDetailsOf' method 38 items compared to what WMIC provides sounds much more manageable. Got any good reading resource on the topic by chance??

All Answers

Do as others have said and use a zip with the MSI and a .BAT file with the GUID to uninstall older versions, but add a way in your script to check for the latest version if you want. Modify the script below to find the registry path of your software, and version number. You can get the product GUID with this command run in PowerShell:
 get-wmiobject Win32_Product | Format-Table IdentifyingNumber, Name, LocalPackage -AutoSize

Install Bat file contents.
@echo off
REM Check registry for latest product version, parse the output to get only the number, and set it as variable
for /f "tokens=3" %%R in ('reg query "HKEY_LOCAL_MACHINE\software\wow6432node\microsoft\windows\currentversion\uninstall\{*****GUID**of**Product**Here*****}" /v DisplayVersion 2^>NUL') do (  set reg_value=%%R
REM If version is "PutLatestVersionHere" then goto the END. If it is not then go to install section
if "%reg_value%"=="PutLatestVersionNumberHereInTheQuotes" (
 goto :END
 ) else (
 goto :INSTALL
REM Uninstall Older version 1 
start /wait msiexec.exe /x {*****GUID**of**Product**Here*****} /qn

REM Uninstall Older version 2
start /wait msiexec.exe /x {*****GUID**of**Product**Here*****} /qn

REM Uninstall Older version 3
start /wait msiexec.exe /x {*****GUID**of**Product**Here*****} /qn

REM Install latest version
start /wait msiexec.exe /i productpackage.msi /qn /l*v C:\Windows\Temp\ProductNameInstall.log /quiet

Answered 11/20/2017 by: john.skinner
White Belt

This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ