2012-12-26 10 views
6

Znalazłem kilka przykładów pokazujących, jak uruchomić skrypt PowerShell z WiX, ale nie udało się uruchomić żadnego z nich. Chciałbym opublikować to, co mam z nadzieją, że ktoś może wskazać, co robię źle.Uruchom skrypt PowerShell z instalatora WiX

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

Kiedy uruchamiam instalator stworzyłem pojawia się następujący błąd (z dziennika):

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. 

nie jestem wcale jasne, co ten błąd próbuje powiedzieć. Czy moje referencje wewnętrzne są złe? Czy polecenie wykonania skryptu jest złe? Coś innego?

Każda pomoc jest najbardziej ceniona i z góry dziękujemy.

+0

Zauważ, że dodanie '-version 2.0' nie mogą dłużej pracować domyślnie w systemie Windows 8/Windows Server 2012. Jest to spowodowane .NET Framework 2.0 (lub 3.5) NIE jest domyślnie instalowany na serwerze. PowerShell 4.0 używa .NET Framework 4.0 (więc nie będzie instalował NetFx2), więc pojawi się błąd, jeśli spróbujesz wykonać 'powershell -Version 2.0'. Najlepiej zrobić skrypt, który działa dla wszystkich wersji PowerShell 2 i nowszych. Zauważ tę odpowiedź http://stackoverflow.com/a/13865175/18475 – ferventcoder

Odpowiedz

4

Wygląda na to, że zaplanowano działanie CAQuietExec jako odroczone. W tym przypadku musisz przekazać wiersz poleceń do wykonania przy pomocy właściwości CustomActionData o nazwie QtExecDeferred, która jest zapisana w skrypcie wykonania. Odroczona akcja może następnie uzyskać dostęp do właściwości ze skryptu.

Więcej szczegółów na http://wixtoolset.org/documentation/manual/v3/customactions/qtexec.html

+0

To wystarczy! To był kawałek, którego mi brakowało, a który nie był widoczny w innych przykładach, na które patrzyłem. Dziękuję Ci bardzo. –

1

nie rozumiałem odpowiedź Stefana, jednak w końcu dostał to działa za pomocą tego bloga post.

Oto podsumowanie zmian zrobiłem do kodu Grega, aby zmusić go do pracy:

  • Zmieniłem CAQuietExec do WixQuietExec (nie jestem pewien, czy było to konieczne).

  • W SetProperty Zmieniłem wartość atrybutu Before z InstallFiles do Id akcji niestandardowych; w przypadku Grega będzie to RunPowerShellScript.

  • Chociaż niezwiązane z pytaniem, skończyło się na konieczności zmiany -Version PowerShell do 3.0 z 2.0 celu uniknięcia błędu podczas uruchamiania mój skrypt.

Tu był mój rzeczywisty kod roboczych:

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