Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run PowerShell script from WiX installer

I have found a couple of examples showing how to run a PowerShell script from WiX but have not been successful running either of them. So, I'd like to post what I have with the hope that someone can point out what I am doing wrong.

<!--Install the PowerShell script-->
<DirectoryRef Id="INSTALLFOLDER">
  <Component Id="cmp_ShutdownIExplore" Guid="{4AFAACBC-97BB-416f-9946-68E2A795EA20}" KeyPath="yes">
    <File Id="ShutdownIExplore" Name="ShutdownIExplore.ps1" Source="$(var.ProjectDir)Source\PowerShell\ShutdownIExplore.ps1" Vital="yes" />
  </Component>
</DirectoryRef>

<!--Define the CustomAction for running the PowerShell script-->
<CustomAction Id="RunPowerShellScript" BinaryKey="WixCA" DllEntry="CAQuietExec" Execute="deferred" Return="check" Impersonate="yes" />

<InstallExecuteSequence>

  <!--Invoke PowerShell script -->
  <Custom Action="RunPowerShellScript" After="InstallFiles"><![CDATA[NOT Installed]]></Custom>
</InstallExecuteSequence>

<!-- Define custom action to run a PowerShell script-->
<Fragment>
  <!-- Ensure PowerShell is installed and obtain the PowerShell executable location -->
  <Property Id="POWERSHELLEXE">
    <RegistrySearch Id="POWERSHELLEXE"
                    Type="raw"
                    Root="HKLM"
                    Key="SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"
                    Name="Path" />
  </Property>
  <Condition Message="This application requires Windows PowerShell.">
    <![CDATA[Installed OR POWERSHELLEXE]]>
  </Condition>

  <!-- Define the PowerShell command invocation -->
  <SetProperty Id="RunPowerShellScript"
           Before ="InstallFiles"
           Sequence="execute"
           Value ="&quot;[POWERSHELLEXE]&quot; -Version 2.0 -NoProfile -NonInteractive -InputFormat None -ExecutionPolicy Bypass -Command &quot;&amp; '[#ShutdownIExplore.ps1]' ; exit $$($Error.Count)&quot;" />
</Fragment>

When I run the installer I have created I get the following error (from log):

MSI (s) (DC:F8) [11:21:46:424]: Executing op: ActionStart(Name=RunPowerShellScript,,)
Action 11:21:46: RunPowerShellScript. 
MSI (s) (DC:F8) [11:21:46:425]: Executing op: CustomActionSchedule(Action=RunPowerShellScript,ActionType=1025,Source=BinaryData,Target=CAQuietExec,)
MSI (s) (DC:9C) [11:21:46:459]: Invoking remote custom action. DLL: C:\Windows\Installer\MSI8228.tmp, Entrypoint: CAQuietExec
CAQuietExec:  Error 0x80070057: failed to get command line data
CAQuietExec:  Error 0x80070057: failed to get Command Line
CustomAction RunPowerShellScript returned actual error code 1603 (note this may not be 100% accurate if translation happened inside sandbox)
Action ended 11:21:46: InstallFinalize. Return value 3.

I am not at all clear what this error is trying to say. Are my internal references bad? Is the command to execute the script bad? Something else?

Any help is most appreciated and thanks in advance.

like image 987
Greg Prosch Avatar asked Dec 26 '12 20:12

Greg Prosch


People also ask

How do I install WiX on Windows?

Step 1: To download and install WiX on windows, go to the official website of WiX as below https://wixtoolset.org/releases and choose the recommended build number, here we have chosen V3. 11.1 and click on the download button.


2 Answers

Only the following example helped me https://github.com/damienbod/WiXPowerShellExample/blob/master/SetupWithPowerShellScripts/Product.wxs

you need to add smth similar into your 'Product.wxs'. the 'Value' property of the first 'CustomAction' contains a ps script (create and run a windows service in my case).

<!-- assign the string (ps command) to RegisterPowerShellProperty -->
<CustomAction Id="RegisterWindowsService"
                        Property="RegisterPowerShellProperty"
                        Value="&quot;C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe&quot; -NoLogo -NonInteractive -InputFormat None -NoProfile sc.exe create MyService binpath= 'C:\Program Files (x86)\My service\MyService.exe';sc.exe start MyService"
                        Execute="immediate" />

<!-- Deferred execution of the above script -->
<CustomAction Id="RegisterPowerShellProperty"
          BinaryKey="WixCA"
          DllEntry="CAQuietExec64"
          Execute="deferred"
          Return="check"
          Impersonate="no" />

<InstallExecuteSequence>
  <!-- On installation we register and start a windows service -->
  <Custom Action="RegisterWindowsService" After="CostFinalize">NOT  Installed</Custom>
  <Custom Action="RegisterPowerShellProperty" After="InstallFiles">NOT Installed</Custom>
</InstallExecuteSequence>

you will need to add a reference to 'WixUtilExtension' in order to run the script.

like image 106
Razor23 Donetsk Avatar answered Oct 05 '22 21:10

Razor23 Donetsk


Looks like you have scheduled the CAQuietExec action as deferred. In this case you have to pass the command line to be executed via a CustomActionData property called QtExecDeferred which is written to the execution script. The deferred action can then access the property from the script.

More details at http://wixtoolset.org/documentation/manual/v3/customactions/qtexec.html

like image 41
Stephen Connolly Avatar answered Oct 05 '22 20:10

Stephen Connolly