0

I try to execute a batch file after setup finishes.

Filename: "{cmd}"; Parameters: "/C ""{app}\Start.bat"""; \
    Description: {cm:LaunchAfterInstallQuest_lbl}; \
    Flags: nowait postinstall skipifsilent shellexec;

The batch file start.bat is requesting administrative privileges and starts two services.

@if (1==1) @if(1==0) @ELSE
@echo off&SETLOCAL ENABLEEXTENSIONS
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"||(
    cscript //E:JScript //nologo "%~f0"
    @goto :EOF
)

REM --> start services
set error=0
set date=%date%_%time:~0,2%-%time:~3,2%-%time:~6,2%

net start "SMC Integrationsserver" 2>>"%~dp0logfile_start_%date%.txt"
IF ERRORLEVEL 1 ( set error=1 )

net start "Wildfly" 2>>"%~dp0logfile_start_%date%.txt"
IF ERRORLEVEL 1 ( set error=1 )

if %error%==1 ( 
echo Ein Fehler trat auf, bitte pruefen Sie die Logdatei "%~dp0logfile_start_%date%.txt"
pause
@goto :EOF
)
if %error%==0 (
del /s /q "%~dp0logfile_*.txt"
)

@goto :EOF
@end @ELSE
ShA=new ActiveXObject("Shell.Application")
ShA.ShellExecute("cmd.exe","/c \""+WScript.ScriptFullName+"\"","","runas",1);
@end

When I click Finish in the setup installer, the start.bat file is executing in a infinite loop.

Any ideas what's wrong with it?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
S.R.
  • 3
  • 1
  • This such an overkill. Why do you request admin privileges in a batch file using such an ugly hack? Why don't you start the services from an elevated environment of the installer? – Martin Prikryl Sep 20 '16 at 13:02

1 Answers1

0

First, this such an overkill. Run the net start (or at least the batch file) directly form the [Run] section of the script, without the postinstall flag. This way the net start (or the batch file) is started with elevated privileges. And you do not need to use that ugly, error-prone and hardly debuggable hack with combining two languages in a single file.

Use "task" to make the run configurable by user.


Anyway, the batch file loops because the Inno Setup is 32-bit application, so it starts 32-bit cmd.exe by default, which cannot see the C:\WINDOWS\system32\config\system on 64-bit systems. That's because for 32-bit processes running on 64-bit systems, the C:\WINDOWS\system32 is redirected to C:\WINDOWS\SysWOW64, where there's no config subdirectory. So the cacls.exe fails with

The system cannot find the file specified.


See these question for details and solutions (hacks in your case):

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thank u Martin. What im trying to do, is to create a file to start and stop my two Services. The user can set the checkbox, to start the service after install or not, this is why i use `postinstall` flag. With this file on the desktop every user can use it, but it needs elevated privileges to start the Service. – S.R. Sep 21 '16 at 06:25
  • First, I believe this [answer to How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?](http://stackoverflow.com/a/12264592/850848) gives a better solution - And anyway, I've shown you how to fix your current script (or rather how to fix the way you run your current script). – Martin Prikryl Sep 21 '16 at 06:35
  • Though in general, I'd use my suggested way (the task). And for the desktop shortcut, I would ideally use the "run as administrator" advanced property of the shortcut (but I admit, I do not know how to set it in Inno Setup). – Martin Prikryl Sep 21 '16 at 06:37
  • I solved it with your last comment. U can set the flag "run as administrator" of the shortcut with innoSetup in the registry like this (http://stackoverflow.com/questions/16083187/how-to-create-a-shortcut-to-launch-an-app-with-admin-privileges-from-the-cmd-lin) – S.R. Sep 21 '16 at 14:50
  • Does it mean you can set the `AppCompatFlags` even for a batch file, not only .exe? – Martin Prikryl Sep 21 '16 at 14:58