Run batch file in Windows Installer Commit

妖精的绣舞 提交于 2019-11-27 22:31:26

What you said above is incorrect:

Custom actions can only be executable files. A batch file is not executable.

Custom Actions (even without tools like InstallShield) can be .EXE, VBScript, JScript, or DLL based. If you write a DLL, you can write whatever code you want to call a batch file or make any changes you want to the system -- there is no limit.

Update: an example that worked for me: (entry in CustomAction table)

Action Test
Type 34
Source SystemFolder
Target cmd.exe /c c:\test.bat
ExtendedType <blank>

I was able to solve this by creating an EXE consisting essentially of:

System.Diagnostics.Process.Start(pathToBatchFile);

Adding the EXE to the MSI file then running it as a custom action.

dashesy

While the answer suggested by "William Leara" is a very good start, I found a better solution here worth mentioning.

Another advantage this method has is that you can put your batch file in the application install folder rather than root C folder, but we use special flags to defer the execution to after FileCopy action, we also use noImpersonate to have higher privilege needed to run from Program Files

A few notes:

  1. Use TARGETDIR instead of INSTALLDIR
  2. Make sure to quote the paths.

These are my two custom actions. In my case I want to force native command processor to fix a few registry entries thus I do not use COMSPEC.

First action (type 50) performs command in Source with input arguments of Target, because Type is 50+1024+2048 to it is deferred action with noImpersonate flag.

  • Action: FinalAction
  • Type: 3122
  • Source: NCOM
  • Target: /c "[TARGETDIR]Fixup.bat"

Second action defines NCOM property immediately:

  • Action: FinalNativeSys
  • Type: 51
  • Source: NCOM
  • Target: [WindowsFolder]System32\cmd.exe

Then you should add these actions to InstallExecuteSequence:

  • Action: FinalAction
  • Condition: NOT Installed
  • Sequence: 5002

And this one:

  • Action: FinalNativeSys
  • Condition:
  • Sequence: 5001

Note that in my case I could easily tweak the actions to call reg.exe import Fixup.reg too. Finally one can use msitran as suggested here in a post build to automate the procedure.

sumenigma

Make an EXE that makes a temporary .bat file to change to the whatever directory you want and CALL another bat file that does what you need.

Tell the custom action to use that exe.

The first argument to a CPP exe is the path to the exe itself. This can be used to orient yourself, and that information could be used in the making of the temporary .bat

In my case, I added "Custom Actions".
On folder "Install" choose "Add custom action"
Choose cmd.exe (c:\windows\system32\cmd.exe)
Then in properties of cmd.exe edit "Arguments" to
"/Install /c C:\myApp.exe --exec-some-action"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!