【问题标题】:Run PowerShell script from WiX installer从 WiX 安装程序运行 PowerShell 脚本
【发布时间】:2012-12-12 07:52:58
【问题描述】:

我找到了几个示例,展示了如何从 WiX 运行 PowerShell 脚本,但没有成功运行其中任何一个。所以,我想发布我所拥有的,希望有人能指出我做错了什么。

<!--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>

当我运行我创建的安装程序时,我收到以下错误(来自日志):

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.

我完全不清楚这个错误想说什么。我的内部参考资料不好吗?执行脚本的命令是否错误?还有什么?

任何帮助都非常感谢,并在此先感谢。

【问题讨论】:

  • 请注意,在 Windows 8/Windows Server 2012 上默认添加 -Version 2.0 可能不再有效。这是由于默认情况下未在服务器上安装 .NET Framework 2.0(或 3.5)。 PowerShell 4.0 使用 .NET Framework 4.0(因此它不会安装 NetFx2),因此如果您尝试执行 powershell -Version 2.0,则会收到错误消息。最好制作一个适用于所有版本的 PowerShell 2 及更高版本的脚本。注意这个答案stackoverflow.com/a/13865175/18475

标签: powershell wix windows-installer


【解决方案1】:

您似乎已将 CAQuietExec 操作安排为延迟。在这种情况下,您必须通过名为 QtExecDeferred 的 CustomActionData 属性传递要执行的命令行,该属性写入执行脚本。然后,延迟的操作可以从脚本中访问该属性。

更多详情http://wixtoolset.org/documentation/manual/v3/customactions/qtexec.html

【讨论】:

  • 成功了!那是我遗漏的部分,在我查看的其他示例中并不明显。非常感谢。
【解决方案2】:

我不明白 Stephen 的回答,但我最终在此博客 post 的帮助下完成了它。

以下是我对 Greg 的代码所做的更改的摘要:

  • 我将CAQuietExec 更改为WixQuietExec(我不确定这是否有必要)。

  • SetProperty我把Before属性的值从InstallFiles改成了自定义动作的Id;在 Greg 的情况下,它将是 RunPowerShellScript

  • 虽然与问题无关,但我最终需要将 powershell 的 -Version2.0 更改为 3.0,以防止在运行我的脚本时出错。

这是我的实际工作代码:

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:iis="http://schemas.microsoft.com/wix/IIsExtension" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
    <Product Id="*" Name="..." Language="1033" Version="..." Manufacturer="..." UpgradeCode="...">
        <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>

        <SetProperty Id="InstallMongoDB"
            Before ="InstallMongoDB"
            Sequence="execute"
            Value="&quot;[POWERSHELLEXE]&quot; -Version 3.0 -NoProfile -NonInteractive -InputFormat None -ExecutionPolicy Bypass -Command &quot;&amp; '[#MONGODB_INSTALL.PS1]' ; exit $$($Error.Count)&quot;" />

        <CustomAction Id="InstallMongoDB" BinaryKey="WixCA" DllEntry="WixQuietExec" Execute="deferred" Return="check" Impersonate="yes" />

        <InstallExecuteSequence>
            <Custom Action="InstallMongoDB" Before="InstallFinalize"><![CDATA[NOT Installed]]></Custom>
        </InstallExecuteSequence>


        <Component Id="MONGODB_INSTALL.PS1" Guid="..." DiskId="1">
            <File Id="MONGODB_INSTALL.PS1" Name="mongodb-install.ps1" Source="mongodb-install.ps1"/>
        </Component>
    </Product>
    <Fragment>
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
                <Directory Id="APPLICATIONFOLDER" Name="...">
                    <Directory Id="InstallScripts" Name="InstallScripts">
                        <Component Id="MONGODB_INSTALL.PS1" Guid="..." DiskId="1">
                            <File Id="MONGODB_INSTALL.PS1" Name="mongodb-install.ps1" Source="mongodb-install.ps1"/>
                        </Component>
                    </Directory>
                </Directory>
            </Directory>
        </Directory>
    </Fragment>
</Wix>

【讨论】:

    【解决方案3】:

    只有以下示例对我有帮助 https://github.com/damienbod/WiXPowerShellExample/blob/master/SetupWithPowerShellScripts/Product.wxs

    您需要在您的“Product.wxs”中添加类似的东西。第一个“CustomAction”的“Value”属性包含一个 ps 脚本(在我的例子中创建并运行一个 Windows 服务)。

    <!-- 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>
    

    您需要添加对“WixUtilExtension”的引用才能运行脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多