-1

I am a system administrator of a server/client environment that doesn't have SCCM and I want to check if computers in my organization have a file name using a batch file.

I want the script to check if the file egui.exe is in one of these directories:

Program Files\ESET\ESET Security\
Program Files (86)\ESET\ESET Security\

If the file EXIST then the script should do nothing.
If the file NOT EXIST then it should start Eset Endpoint installation in quiet mode from my server:

msiexec /i myserver\eea_nt64_enu.msi /qb 

I wrote something, but I'm not sure if it is correct and complete:

If exist "C:\program files\ESET\ESET Security\egui.exe" echo ?
else 
call msiexec /i myserver\eea_nt64_enu.msi /qb

Again, I do not want the script to load the installation if the file exists. If the executable exists in that folder, then the batch file should do nothing.

How to check existence of executable in Program files and Program files (x86)?

How to check what version each one has?

If the version of the AntiVirus is old (let's say version 4) then it should force it to install AND override the current version?

Mofi
  • 46,139
  • 17
  • 80
  • 143
StackBuck
  • 789
  • 1
  • 12
  • 34
  • I bet if you read the help file for the IF command you will see what the proper syntax is for using ELSE. – Squashman Apr 11 '18 at 12:41
  • Well, i dont find it. Also, cant find how to make it not to do anything if the file exists. – StackBuck Apr 11 '18 at 13:20
  • 1
    @StackBuck, it's not difficult to find the help on a particular command, open a command prompt, `cmd.exe`, window then type `command /?` and press the enter key. In this case you'd type `if /?` for information on the `If` command. – Compo Apr 11 '18 at 13:42
  • OK. How do i make the script "do nothing" if the file exists? – StackBuck Apr 11 '18 at 13:48
  • 1
    Why not use IF NOT EXIST? https://stackoverflow.com/questions/6780553/if-not-exist-cmd-command-not-working – Adam Leinss Apr 11 '18 at 13:50
  • Thanks adam! Please consider make it as an answer so i will accept and vote! – StackBuck Apr 11 '18 at 13:51
  • @StackBuck you seriously cannot see the syntax example in the help file for the `IF` command. It is exactly what you want. – Squashman Apr 11 '18 at 13:55

2 Answers2

1

I assume you have verified that the MSI in question does not take care of this "problem" on its own? It might have the necessary logic to clean up older versions built into it so you don't need to do any checking of pre-conditions?

I see you have gotten a batch answer (IF NOT EXIST), but I want to add that you can solve this using legacy, active scripting (VBScript, Javascript - I see you might be a Javascript man), or more modern Powershell scripts (which I don't use much).

Can we ask how you are invoking the install? Via logon script, scheduled tasks, AD or some other mechanism?

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
  • Im a littel bit on Powershell scripts but for your question im planning doing it via logon script. – StackBuck Apr 11 '18 at 13:59
  • OK, Powershell is not my thing, but here is a good source: https://github.com/heaths/psmsi (probably known to you). And if you haven't already, maybe just run the setup directly on a system with the pre-existing file and see what happens. You probably already did. – Stein Åsmul Apr 11 '18 at 14:06
1

Open a command prompt window and run if /? and goto /? to understand the following batch file:

@echo off

rem This first file existence check works on 32-bit and
rem 64-bit Windows in 32-bit and 64-bit environment.
if exist "%ProgramFiles%\ESET\ESET Security\egui.exe" goto :EOF

rem On 64-bit Windows run also a file existence check for 32-bit version
rem of application in standard program files folder for x86 applications.
if not "%ProgramFiles(x86)%" == "" if exist "%ProgramFiles(x86)%\ESET\ESET Security\egui.exe" goto :EOF

rem If this batch file is executed by 32-bit cmd.exe on 64-bit Windows
rem there is still not checked if 64-bit version of application exists
rem in standard program files folder for x64 applications.
if not "%ProgramW6432%" == "" if exist "%ProgramW6432%\ESET\ESET Security\egui.exe" goto :EOF

rem Other commands to install the application depending on Windows architecture.

Read the Microsoft article WOW64 Implementation Details for the reason of all those IF conditions.

But I suppose it would be better to check existence of egui.exe by querying application registration registry key of this application installed with a Microsoft Installer package.

@echo off
%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\egui.exe" >nul 2>&1
if not errorlevel 1 goto :EOF

rem Other commands to install the application depending on Windows architecture.

REG exits with value 1 if the specified registry key does not exist and with value 0 on success querying this registry key.

if not errorlevel 1 means if exit code of previously executed command/application is not greater or equal 1 which means lower than 1 which means for nearly all commands and applications being equal 0.

I don't have ESET Security package installed and so don't know if egui.exe (or any other executable from this package) is registered according to Microsoft guidelines for Windows application registration.

For understanding all used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • goto /?
  • if /?
  • reg /?
  • reg query /?
  • rem /?
Mofi
  • 46,139
  • 17
  • 80
  • 143