2012-12-26 90 views
6

我發現了幾個示例,展示瞭如何從WiX運行PowerShell腳本,但未能成功運行它們中的任何一個。所以,我想發佈我所期望的,希望有人能指出我做錯了什麼。從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. 

我並不清楚這個錯誤是什麼想說的。我的內部引用不好嗎?命令是否執行腳本不好?還有別的嗎?

任何幫助是最感謝和提前感謝。

+0

注意添加'-Version 2.0'可能不再默認情況下,在Windows上工作8/Windows的Server 2012的這是由於.NET Framework 2.0的(或3.5)默認情況下不安裝在服務器上。 PowerShell 4.0使用.NET Framework 4.0(因此它不會安裝NetFx2),所以如果您嘗試執行'powershell -Version 2.0',則會出現錯誤。最好製作適用於所有版本的PowerShell 2及更高版本的腳本。注意這個答案http://stackoverflow.com/a/13865175/18475 – ferventcoder

回答

4

看起來您已將CAQuietExec操作安排爲延期。在這種情況下,您必須通過命令行來通過名爲QtExecDeferred的CustomActionData屬性執行,該屬性被寫入執行腳本。延遲操作可以從腳本訪問屬性。

更多細節在http://wixtoolset.org/documentation/manual/v3/customactions/qtexec.html

+0

這個伎倆!那是我失蹤的那部分,在我看過的其他例子中並不明顯。非常感謝你。 –

1

我不明白斯蒂芬的回答,但是我最終得到了這本博客post的幫助工作。

下面是我對格雷格的代碼所做的更改的摘要,以得到它的工作:

  • 我改變CAQuietExecWixQuietExec(我不知道這是否是必要的)。

  • SetProperty我將Before屬性的值從InstallFiles更改爲自定義操作的Id;在格雷格的情況下,它將是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>