首页 文章

从WiX中静默执行PowerShell脚本挂起PowerShell

提问于
浏览
10

注意:此问题也发布在WiX Users mailing list上 .

我试图从WiX生成的MSI静默执行PowerShell脚本 . 但是,无论何时我运行安装程序,PowerShell都会挂起 . 有趣的是,根据安装程序日志,PowerShell脚本似乎成功运行 . 此外,如果我通过任务管理器终止PowerShell进程,安装程序将取消安装并回滚所有更改 .

PowerShell Script Contents

# @param website The website under which the module should be compiled and registered.
# @param name The name of the module to be registered.
# @param assembly The assembly name, version, culture and public key token to be compiled.
# @param assemblyType The fully qualified assemebly type to be registered.

param([string]$website = "website", [string]$name = "name", [string]$assembly = "assembly", [string]$assemblyType= "assemblyType")

import-module webadministration
add-webconfiguration /system.web/compilation/assemblies "IIS:\sites\$website" -Value @{assembly="$assembly"}
new-webmanagedmodule -Name "$name" -Type "$assemblyType" -PSPath "IIS:\sites\$website"

WiX Custom Action Contents : Attempt 1

我的第一次尝试是使用&special字符来执行脚本 .

<CustomAction Id="RegisterHttpModulePSCmd"
              Property="RegisterHttpModulePowerShellProperty"
              Value="&quot;C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe&quot; &amp;'C:\Program Files (x86)\My Company\Scripts\register-httpmodule.ps1' -website 'Default Web Site' -name 'MyCustomModule' -assembly 'MyCompany.Product.Feature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx' -assemblyType 'MyCompany.Product.Feature.MyModule'"
              Execute="immediate" />

<CustomAction Id="RegisterHttpModulePowerShellProperty"
              BinaryKey="WixCA" 
              DllEntry="CAQuietExec64" 
              Execute="deferred"
              Return="check" 
              Impersonate="no" />

<InstallExecuteSequence>
   <Custom Action="RegisterHttpModulePSCmd" After="CostFinalize">NOT  Installed</Custom>
   <Custom Action="RegisterHttpModulePowerShellProperty" After="InstallFiles">NOT Installed</Custom>
</InstallExecuteSequence>

WiX Custom Action Contents : Attempt 2

我的第二次尝试是使用-File参数来执行脚本 .

<CustomAction Id="RegisterHttpModulePSCmd"
              Property="RegisterHttpModulePowerShellProperty"       
              Value="&quot;C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe&quot; -NoLogo -NonInteractive -NoProfile -File &quot;C:\Program Files (x86)\My Company\Scripts\register-httpmodule.ps1&quot; -website &quot;Default Web Site&quot; -name &quot;MyCustomModule&quot; -assembly &quot;MyCompany.Product.Feature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx&quot; -assemblyType &quot;MyCompany.Product.Feature.MyModule&quot;"
              Execute="immediate" />

<CustomAction Id="RegisterHttpModulePowerShellProperty"
              BinaryKey="WixCA" 
              DllEntry="CAQuietExec64" 
              Execute="deferred"
              Return="check" 
              Impersonate="no" />

<InstallExecuteSequence>
   <Custom Action="RegisterHttpModulePSCmd" After="CostFinalize">NOT  Installed</Custom>
   <Custom Action="RegisterHttpModulePowerShellProperty" After="InstallFiles">NOT Installed</Custom>
</InstallExecuteSequence>

这两种方法似乎都可以在对所需的web.config文件进行修改时起作用,但是,这两种方法都会挂起PowerShell,从而挂起安装程序 .

其他信息

我修改了PowerShell脚本以打印出版本信息,而不执行任何其他操作 . 然后,MSI日志文件显示以下内容:

MSI (s) (D4:78) [10:26:31:436]: Hello, I'm your 32bit Elevated custom action server.
CAQuietExec64:  
CAQuietExec64:  
CAQuietExec64:  Name             : ConsoleHost
CAQuietExec64:  Version          : 2.0
CAQuietExec64:  InstanceId       : 62b0349c-8d16-4bd1-94e5-d1fe54a9ff54
CAQuietExec64:  UI               : System.Management.Automation.Internal.Host.InternalHostUserI
CAQuietExec64:                     nterface
CAQuietExec64:  CurrentCulture   : en-US
CAQuietExec64:  CurrentUICulture : en-US
CAQuietExec64:  PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
CAQuietExec64:  IsRunspacePushed : False
CAQuietExec64:  Runspace         : System.Management.Automation.Runspaces.LocalRunspace

此时安装程序似乎已停止,因为PowerShell未退出 . 当我使用任务管理器手动杀死PowerShell时,接下来的几条日志消息是:

CAQuietExec64:  Error 0x80070001: Command line returned an error.
CAQuietExec64:  Error 0x80070001: CAQuietExec64 Failed CustomAction RegisterHttpModulePowerShellProperty returned actual error code 1603 (note this may not be 100% accurate if translation happened inside sandbox) Action ended 10:27:10: InstallFinalize. Return value 3.

如何在不挂起PowerShell的情况下从Wix静默执行PowerShell脚本?

2 回答

  • 18

    我在搜索一个不相关的问题时看到了几个这样的帖子,发现了一个answer . 底线:在命令行中包含标志 -InputFormat None .

  • 0

    您是否尝试将关键字 exit 添加到脚本的末尾?

    我遇到过与我一直在研究的C#项目类似的情况 . 构建项目后,我使用MSBuild的 <Exec> 任务在 <AfterBuild> 目标中调用PowerShell,指定要运行的脚本 . 如果脚本不包含 exit 关键字,VS2010 "hangs" - 也就是说它实际上正在等待PowerShell完成其任务,而PowerShell则等待用户输入 .

    如果您打开任务管理器或SysInternals Process Explorer,您将看到 powershell.exe 正在运行,好像没有错 . 如果你终止进程,VS2010(即MSBuild)会抛出错误 .

    因此,您需要通过将 exit 添加到它运行的脚本来告诉PowerShell您已完成它,并且一切都应该再次正常 .

相关问题