Search code examples
windowsinstallationwixwix4

How to log stderr from Windows bat files in Wix 4


Is there a way to get Wix 4 to log stderr (and stdout) from bat files executed during a Bundle installation?

Scenario:

I have a Wix Bundle that executes a bat file to create a database using SqlLocalDB:

<ExePackage
  SourceFile="ConfigureDB.bat">
</ExePackage>

The content of the bat file is something like this:

cmd.exe --parameters1
cmd.exe --parameters2
cmd.exe --parameters3

Sometimes the bat script fails and the cmd.exe outputs relevant error information to stderr. To capture the error information I currently redirect output like this:

cmd.exe --parameters1 > C:\tmp\c1a.txt 2> C:\tmp\c1b.txt
cmd.exe --parameters2 > C:\tmp\c2a.txt 2> C:\tmp\c2b.txt
cmd.exe --parameters3 > C:\tmp\c3a.txt 2> C:\tmp\c3b.txt

That is not really useful once this MSI package is released, so the question is: how do I ensure the Wix installer captures the output of commands inside a bat script?


Solution

  • Bundles expect packages to log themselves (as you are doing) so there is no mechanism to capture stdout/stderr back to the bundle's log file. IIRC, there is a Variable set for each package that can be used as the location where to log. I think you will see it being set in the Bundle log file.

    EDIT

    To set a variable containing the logfile name, use this:

    <Log PathVariable="WixBundleLog"/>
    

    The variable WixBundleLog can then be passed to executables and batch files:

    <ExePackage
      SourceFile="ConfigureDB.bat"
      InstallArguments="[WixBundleLog]-MYLOG.txt">
    </ExePackage>
    

    The syntax "[X]" replaces [X] with the content of the variable X.

    Append something like "-MYLOG.txt" to the logfile name as the actual logfile is locked for other programs to write into.

    And for good measure - here is how to write stderr+stdout to the same logfile. Assuming logfile name has been passed as first argument to the batch file, then %1 is logfile name, so >> redirects stdout to it and 2>&1 redirects stderr to stdout:

    DoStuff >> %1 2>&1