如何在正确访问 Windows 注册表的情况下在 WiX 中启动 PowerShell?

cod*_*ero 5 registry powershell windows-installer wix wix3.11

更新

有趣的是,如果我运行 32 位 powershell 来运行脚本,它会给我同样的错误。看起来 32 位的 powershell 无法访问 64 位的注册表树?我尝试使用WixQuietExec64但它给出了同样的错误。我还尝试提供 powershell ( C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe)的完整路径以确保安装程序启动 64 位版本,但仍然出现相同的错误......看起来这可能是由 MSI 安装程序本身为 32 位引起的??

MSI (s) (4C:C0) [14:25:49:955]: Hello, I'm your 32bit Elevated Non-remapped custom action server.
Run Code Online (Sandbox Code Playgroud)

原帖

我有以下test.ps1脚本:

$exchangeroot = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ExchangeServer\"
$allexchanges = Get-ChildItem -Path Registry::$exchangeroot -Name | Where-Object { $_ -match "^V.." }
$sorted = $allexchanges | Sort-Object -descending
If ($sorted.Count -gt 1) { $latest = $sorted[0] } Else { $latest = $sorted }
$setup = $exchangeroot + $latest + "\Setup"
$properties = Get-ItemProperty -Path Registry::$setup
$properties
Run Code Online (Sandbox Code Playgroud)

在普通的 PowerShell 窗口中运行脚本会产生以下输出:

PS C:\Program Files (x86)\TrustValidator Exchange Server Plugin> .\test.ps1

Required machine-level settings.          : 1
Services                                  : C:\Program Files\Microsoft\Exchange Server\V15
NewestBuild                               : 10845
CurrentBuild                              : 710737954
Information Store Service                 : 1
Messaging and Collaboration Event Logging : 1
MsiInstallPath                            : C:\Program Files\Microsoft\Exchange Server\V15\
...
Run Code Online (Sandbox Code Playgroud)

所以它有效。现在从 WiX 安装程序启动 PowerShell 并执行脚本,它不会生成相同的结果:

WixQuietExec:  Get-ItemProperty : Cannot find path 
WixQuietExec:  'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ExchangeServer\v15\Setup' because it 
WixQuietExec:  does not exist.
WixQuietExec:  At C:\Program Files (x86)\TrustValidator Exchange Server Plugin\test.ps1:10 
WixQuietExec:  char:16
WixQuietExec:  +     $properties = Get-ItemProperty -Path Registry::$setup
WixQuietExec:  +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
WixQuietExec:      + CategoryInfo          : ObjectNotFound: (HKEY_LOCAL_MACH...erver\v15\Set 
WixQuietExec:     up:String) , ItemNotFoundException
WixQuietExec:      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetIt 
WixQuietExec:     emPropertyCommand
Run Code Online (Sandbox Code Playgroud)

现在,如果我们观察到错误消息,就好像它可以访问树直到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ExchangeServer\,因为我的脚本会搜索并列出所有版本,所以v15必须可以访问到那个点,但是当它试图更深入地访问得到ItemProperty,它不能。

这让我相信,从 WiX 安装程序启动我的 PowerShell 时,我可能遗漏了什么......?

这是我的 wxs 文件中的内容:

<SetProperty Id="InstallPlugin"
     Before ="InstallPlugin"
     Sequence="execute"
     Value ="&quot;powershell.exe&quot; -Command &quot;cd '[INSTALLFOLDER]'; &amp; '[#TestPS1]' ; exit $$($Error.Count)&quot;" />
<CustomAction Id="InstallPlugin" BinaryKey="WixCA" DllEntry="WixQuietExec" Execute="deferred" Return="ignore" Impersonate="no" />
Run Code Online (Sandbox Code Playgroud)

以下是我已经尝试或仔细检查过的项目列表:

  • 我尝试了 , 的不同组合-NoProfile-ExecutionPolicy ByPass-Version 2.0仍然不好。
  • 我已经在运行安装程序 InstallPrivileges="elevated"
  • 我已经在运行CustomActionasExecute="deferred"Impersonate="no"
  • 我试过 AdminImage="yes"
  • 我试过设置 <Property Id="MSIUSEREALADMINDETECTION" Value="1" />

任何其他线索将不胜感激。:(

cod*_*ero 6

我的天啊...

好的,我终于让它工作了。实际上有几个问题,这些问题的解决方案实际上是我从多个 SO 问题中收集的零碎信息。

回顾一下,这就是我想要做的:

  1. 从 WiX 启动一个 powershell 来运行我的安装脚本。
  2. 我的脚本在 Windows 注册表(需要 64 位)中搜索已安装的 Exchange Server 的位置
  3. 我的脚本从安装位置加载 Exchange 命令行管理程序 (EMS) 脚本(需要 64 位和适当的用户)
  4. 在 EMS 会话下,我的脚本运行其他脚本的早午餐来注册 Exchange 插件

问题 1)

无论我做什么,WiX 安装程序总是以 32 位启动我的 powershell,这与设置Platform="x64"Win64="yes"、甚至WixQuietExec64. 我甚至建在Visual Studio中,安装程序x64一切作为x64

解决办法是直接引用sysnativepowershell,它必须sysnativeSetProperty.

C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe
Run Code Online (Sandbox Code Playgroud)

我之前确实尝试过这个,并认为它不起作用,但根本原因被下面的问题 2 掩盖了。

问题 2)

在我读到的任何地方,他们都说你需要使用Execute="deferred" Impersonate="No". 我相信如果你没有做任何时髦的事情,这确实适用于大多数情况。但是,我不得不 Impersonate。我发现 WiX 安装程序会以用户NT Authority/System. 这把我搞砸了,因为Exchange Management Shell我试图获取的脚本基本上会使用您的凭据并尝试与 Exchange Server 建立会话……当然,您无法以NT Authority/System!

该解决方案是使用Impersonate="yes"这样的WiX的安装程序将运行CA的升高,并且您当前已登录。用户我总的印象是,你必须使用Impersonate="no"使用时Execute="deferred"...但你不知道

我放弃了几天的故障排除,然后回到它并让它工作。帮助我解决这个问题的 2 个最有用的命令实际上是:

  • 我是谁
  • Get-ChildItem env:(检查 PROCESSOR_ARCHITECTURE)