【问题标题】:Silently executing a PowerShell script from WiX Hangs PowerShell从 WiX 静默执行 PowerShell 脚本会挂起 PowerShell
【发布时间】:2011-05-07 12:04:20
【问题描述】:

注意:这个问题也发布在WiX Users mailing list

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

PowerShell 脚本内容

# @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 自定义操作内容:尝试 1

我的第一次尝试是使用 & 特殊字符来执行脚本。

<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 自定义操作内容:尝试 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 脚本?

【问题讨论】:

    标签: powershell wix windows-installer wix3.5


    【解决方案1】:

    您是否尝试在脚本末尾添加关键字exit

    我在一个 C# 项目中遇到了类似的情况。构建项目后,我使用 MSBuild 的 &lt;Exec&gt; 任务在 &lt;AfterBuild&gt; 目标中调用 PowerShell,指定要运行的脚本。如果脚本不包含 exit 关键字,VS2010 “挂起” - 也就是说它实际上是在等待 PowerShell 完成其任务,而 PowerShell 则等待用户输入。

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

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

    【讨论】:

    • 我尝试在末尾添加退出,不幸的是这对 PowerShell 进程是否终止没有影响。
    • :-/ 你如何调用 PowerShell,你传递给它什么参数?
    • 尝试将 -NoProfile 开关与 -File "" 结合使用,或者(不太理想)使用 -Command 或 -EncodedCommand 将脚本内容作为脚本块传递,确保关键字'出口;'附加到末尾。您甚至可以使用其中任何一个参数对脚本进行点源。您可以通过键入“powershell.exe /?”来获取有关参数列表的信息。从 PowerShell 或命令提示符中。
    【解决方案2】:

    我在搜索一个不相关的问题时看到了几篇类似的帖子,并找到了answer。底线:在命令行中包含标志 -InputFormat None

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 2011-07-16
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多