Script help - msiexec based on 32/64 bit OS

Dear experts,

I need some help putting together a script that will check whether the client OS is 32-bit or 64-bit and depending on the result it will install an MSI package.

I had a quick search on the internet but a lot of the results come up with finding whether the processor architecture is 32 or 64 bit rather than whether the OS is.

So essentially what I need is something like:

If Package "Application X" is installed then go to end

If machine has 32-bit OS
Then msiexec \\Path\Packagex86.msi /q /norestart

If machine has 64-bit OS
Then msiexec \\Path\Packagex64.msi /q /norestart
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.

John EastonDirectorCommented:
Assuming your are creating a simple .bat file then you could check if the folder C:\Program Files (x86) exists.  This folder only exists on 64-bit systems.  Not a guarentee as someone could manually create this folder, but should be ok in most cases.  Therefore a script would look something like:

IF EXIST "C:\Program Files (x86)" GOTO Run64

msiexec \\Path\Packagex86.msi /q /norestart

GOTO End

:Run64
msiexec \\Path\Packagex64.msi /q /norestart

:End

Open in new window


Also, to test for the program install you can add IF EXIST cases to test.  You may need to do this within the 32-bit section and the 64-bit section as install locations may be different.  For example add:
IF EXIST "C:\Program Files (x86)\MyApp" GOTO End

Open in new window

0
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Don't do this by checking whether a directory exists, there are a host of reasons why the directory may exist when the OS is in fact running 32bit.

In windows NT the "%Processor_Architecture%" variable will return whether the environment is 32bit (x86) or 64bit (AMD64). This means the OS is running 32bit or 64 bit regardless of the actual hardware on the computer.

(Is.  A 32bit OS installed on a machine with a brand new Intel i7 or AMD processor will return x86, even though the CPU is 64 bit)

Generally this is the most effective method to find the bit level as normally a batch file will run in the native bit level of the OS.

There is one caveat, and its quite unusual; if you run a 32bit application which calls the batch, depending on how it's called it may get it's local version of this variable set to x86 instead of AMD64. ( I have yet to experience this issue personally, but I've read about it before.)

To guard against this one issue that could occur to make the variable incorrect, you can query the registry to see what the OS is natively running as instead:
reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v PROCESSOR_ARCHITECTURE

Open in new window

0
John EastonDirectorCommented:
@QCubed:  Didn't know that variable existed - good to know.  The rest of my code should still stand therefore to save to OP some effort:

IF %Processor_Architecture% == AMD64 GOTO Run64

msiexec \\Path\Packagex86.msi /q /norestart

GOTO End

:Run64
msiexec \\Path\Packagex64.msi /q /norestart

:End

Open in new window

0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Oh, also, on versions of windows NT which do not offer a 64bit version, the registry key and variable do no exist, but you can check for this condition using IF DEFINED.

Here is a batch file that checks to see if the process architecture is defined, if not, runs 32bit install, if it is, it runs the reg query to see if it can find "x86", if it can it runs the 32 bit MSI, if it can't it runs the 64bit MSI.
:: Script Name: RunMSI64x86.bat
:: Version: 1.0
:Begin
@(
  SETLOCAL
  ECHO OFF
  SET "MSIPath=\\path"
  SET "MSI64=Packagex64.msi"
  SET "MSI86=Packagex86.msi"
  SET "eLvl=0"
)

Call :Main

:End
(
  ENDLOCAL
  EXIT /b %eLvl%
)

:: Subroutines Follow:

:Main

  IF NOT DEFINED Processor_Architecture (
    MSIExec "%MSIPath%\%MSI86%"/q /norestart
  ) ELSE (
    reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v PROCESSOR_ARCHITECTURE | (
      (
        Find /I "x86" && (
          MSIExec "%MSIPath%\%MSI86%"/q /norestart
        )
      ) || (
        MSIExec "%MSIPath%\%MSI64%"/q /norestart
      )
    )  
  )
GOTO :EOF

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
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@JEaston, cool, always looking to help.

@OP: FYI, I'm writing on my android, so I have not yet tested the variable, please review for any spelling mistakes etc.
0
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@JEaston, looking at your code, always wrap paths and variables used in compares within quotes, otherwise if they contain spaces they will cause syntax issues.
0
Bladey001Author Commented:
Thanks guys

Is it possible to add the part near the beginning where if the application is already installed it ends the script? I suppose otherwise it will try and run the msi on startup each time
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.