[wix-users] Executing Batch File

Edwin Castro egcastr at gmail.com
Thu Apr 5 07:13:48 PDT 2018


The documentation describing how to reference the full path to a file
getting installed is at

https://msdn.microsoft.com/en-us/library/windows/desktop/aa368609.aspx

The WiX documentation doesn't cover this because it is a Windows Installer
concept. To be fair, the WiX documentation should probably have links to it
so that newbies can find it easier.

The Windows Installer logically performs multiple passes of the
InstallExecuteSequence. In the first "pass", only immediate mode custom
actions are executed (with full access to tables and properties) and an
execute script is generated. The second "pass" occurs when the generated
execute script is executed when deferred custom actions are executed (with
access only to custom action data and a very limited number of properties).
The third "pass" only executes if a deferred action fails. In this last
pass, the generated execute script is executed in reverse starting from the
action prior to the action that failed but only rollback actions are
executed.

The purpose if the first "pass" is to make decisions on what actions should
be taken. The second "pass" actually modifies the target system. And the
third "pass" rollsback in case of failure.

Your WixQuietExec action for running bcpImp.bat must be deferred (runs in
the second pass). The SetProperty for the command line implements an
immediate custom action (first pass) to set the custom action data for the
deferred action. You'll need to consider what should happen, if anything,
during rollback in the third pass (if you need a rollback action it must be
scheduled before your deferred action).

One consequence of all this is that you cannot communicate between deferred
actions!! Since you want the return code only to determine if it succeded
then you can simply set Return="check" on the CustomAction element as per
the WiX documentation. This ensures that the Windows Installer waits for
the deferred action to complete (it runs synchronously) and checks the
return code to be zero indicating success. With this you just need to
schedule the follow up scripts/commands after this one and the later ones
only get executed if the previous ones succeeded.

Additionally, if and only if, your .bat script only executes a single
command, then you can use WixQuietExec to run the command directly instead
of using the .bat script. Obviously, if your .bat script runs multiple
commands, then you'll definitely want to use the .bat script. Just ensure
the .bat only returns a zero exit code on success and non-zero on failure.

--
Edwin G. Castro



On Thu, Apr 5, 2018, 05:38 Joel McBeth via wix-users <
wix-users at lists.wixtoolset.org> wrote:

> Here is the documentation for using the Execution custom action:
> http://wixtoolset.org/documentation/manual/v3/customactions/qtexec.html
>
> CAQuietExec is being depreciated for WixQuietExec. You don't use
> ExeCommand with WixQuietExec. I would look at the examples in the link
> above.
>
> Also a tip, if you are installing bcpimp.bat you can directly reference
> the file by ID. I couldn't quickly find the documentation on this with a
> good example, but check out this stack overflow:
> https://stackoverflow.com/questions/9664652/how-do-i-reference-the-installed-path-of-a-file-in-a-registry-value
>
> Although that might all be pointless as I don't think you can capture the
> output of WixQuietExec, and if you could I'm not sure you could easily
> parse it without a custom action.  That being said, you should probably
> just write your own custom action to do all of this instead of using
> WixQuietExec.
>
> For point 3, you can probably schedule the BCP custom action to run before
> the SQL scripts are installed.
>
> -----Original Message-----
> From: wix-users [mailto:wix-users-bounces at lists.wixtoolset.org] On Behalf
> Of Ven H via wix-users
> Sent: Thursday, April 5, 2018 3:34
> To: WiX Toolset Users Mailing List <wix-users at lists.wixtoolset.org>
> Cc: Ven H <venh.123 at gmail.com>
> Subject: [wix-users] Executing Batch File
>
> I am trying to execute a batch file through my MSI. I am using the BuiltIn
> Custom Action with the following code. The batch file is meant for BCP.
> Hence I am passing the arguments to the command using properties, as given
> below in Product.wxs
>
> <CustomAction Id="test" ExeCommand="[INSTALLFOLDER]bcpImp.bat
> "[SQLSERVER]\[SQLINSTANCE]"  "[SQLUSER]"
> "[SQLPASSWORD]"  "[SQLDB]"  "[SQLTABLE]"
> "[BCPIMPFILE]"" Execute="deferred" Return="check"
> Directory="INSTALLFOLDER"  />
>
> <InstallExecuteSequence>
>       <Custom Action="test" After="InstallFiles">NOT Installed</Custom>
>     </InstallExecuteSequence>
>
> This works fine and the data gets imported. But I have a couple of issues.
>
> 1. During execution, it pops up a Console Window. To suppress it, if I use
> DllEntry="CAQuietExec", I get an error that "The CustomAction element may
> only have one of the following target attributes specified at a time." So,
> I cannot have DllEntry and ExeCommand specified at the same time in my
> CustomAction element. Hence I am not able to execute the BCP command
> quietly. Is this achievable?
>
> 2. To understand whether the custom action executed successfully and to
> capture it's return value, I tried assigning a Property. In that case I had
> to remove the Directory attribute, otherwise, it was not getting compiled.
> But, I got a runtime exception "A program required for this install could
> not be run.". So I am not sure how to read the return value. I need this
> because, I want to execute some more SQL scripts, after the data is
> imported through BCP. So, I want to check if the BCP import ran
> successfully, then accordingly, I want to execute more sql scripts. Is this
> possible?
>
> 3. To reiterate the second part of the above issue, I would like to
> execute some sql scripts after the execution of the custom action. Is this
> possible? If yes, how?
>
> Please help.
>
> ____________________________________________________________________
> WiX Toolset Users Mailing List provided by FireGiant
> http://www.firegiant.com/
>
> ____________________________________________________________________
> WiX Toolset Users Mailing List provided by FireGiant
> http://www.firegiant.com/
>



More information about the wix-users mailing list