Sven Keller
Sven Keller

Reputation: 621

Wix installer custom action powershell script not properly executed

During the installation I need to customize some configuration files which is basically to search and replace certain keywords given as Properties to the msi intaller. The custom action looks like this:

 <CustomAction Id="SetApplicationProperties"
    Directory="CONFIG.DIR"
    ExeCommand="powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command &quot;cat application.properties.template | % { $_ -replace 'SERVERNAME','[SERVERNAME]' } > application.properties.customer&quot; "
    Execute="deferred"
    Impersonate="no"
  />

However I only get an empty "application.properties.customer" file. no error/warning in installer logfile. I tried various combination of quoting the strings but w/o success. Reducing the command to:

ExeCommand='powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command "cat application.properties.template  > application.properties.test" '

works, so it seems to be a problem with the quoting of the "-replace..." statement.

Has anyone any sugestions how properly set the quotes for the installer?

Upvotes: 1

Views: 1200

Answers (2)

Sven Keller
Sven Keller

Reputation: 621

Meanwhile I found a solution for the issue myself after some trial and error I figured out that the '{' and '}' were eliminated during "transfer" from project to execution. I've found this link helpful as starting point for further experiments. The result is that [\{] VALUE [\}] can be used to "escape" the parenthesis. Working code now looks like:

  ExeCommand="powershell -NoLogo -NoProfile -NonInteractive -InputFormat None -ExecutionPolicy Bypass -Command &quot;cat application.properties.template | % [\{] $_ -replace '&lt;SERVERNAME&gt;','[SERVERNAME]' [\}] > application.properties.customer 2>&amp;1 &quot; "

Upvotes: 1

Stein &#197;smul
Stein &#197;smul

Reputation: 42136

PowerShell CAs Considered Harmful: Using PowerShell for custom actions is a very bad runtime requirement to trigger. It is both a script AND it is managed code - both are problematic in its own right. Scripts are generally hard to debug and can trigger anti-virus lockups, and managed code depends on the .NET runtime and has a number of well-known technical pitfalls (versioning issues, load behavior of CLR version, there are many more issues...). Avoid PowerShell custom actions if you can.

XML Updates: Is this an XML file? If so, maybe you can try WiX's own XML update features? Maybe see this answer: Added new application setting to app.config but MSI won't install it (doesn't overwrite).

JSON: If it is JSON let me lob you just a link, not used by me so far. All I found in my bookmarks. Pillaging github.com is a common approach for me. Just do a search.

INI: Maybe it is even in INI format? If so I have no samples for you. Commercial tools InstallShield and Advanced Installer allow you to retrieve values from INI files. MSI's built-in features are probably too quirky to do so (Encoding issues, limited feature set). I am not sure if WiX has extended support to retrieve INI file values.

Upvotes: 0

Related Questions