gpt4 book ai didi

在 x86 上下文中运行时,PowerShell 返回已安装软件的不准确结果

转载 作者:行者123 更新时间:2023-12-03 16:24:55 25 4
gpt4 key购买 nike

我编写了以下脚本来检查应用程序名称并回显某些属性(如果已安装):

$input = "Microsoft Office Professional"
$appName = "*" + $input + "*"

$responseX64 = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -like $appName} | Select-Object @{Expression={$_.DisplayName + "|" + $_.DisplayVersion +"|x64"}} | Sort-Object -Unique | ft -HideTableHeaders
$responseX86 = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -like $appName} | Select-Object @{Expression={$_.DisplayName + "|" + $_.DisplayVersion +"|x86"}} | Sort-Object -Unique | ft -HideTableHeaders

If (([string]::IsNullOrEmpty($responseX64)) -and ([string]::IsNullOrEmpty($responseX86)))
{
Write-Output "No matches found."
exit
}
else
{
Write-Output $responseX64
Write-Output $responseX86
}

为了完整性,我检查了 x86 和 x64 卸载注册表项。当我在 x64 PowerShell session 中运行它时,它按预期工作,并正确返回我安装了单个 x86 Microsoft Office:

Microsoft Office Professional Plus 2016|16.0.4266.1001|x86

但是,当我在同一台计算机上的 PowerShell x86 session 中运行它时(因为它将由我的 x86 管理代理运行),我得到以下返回:

Microsoft Office Professional Plus 2016|16.0.4266.1001|x64

Microsoft Office Professional Plus 2016|16.0.4266.1001|x86

我仔细检查了 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 下的注册表项,以确保其中没有 Office 的某些 x64 工件,但实际上什么也没有。如何修改此脚本,以便在 x86 上下文中运行时返回准确的结果?

最佳答案

您的问题是,对于 32 位进程,以下关键路径引用相同位置,即 32 位应用程序的注册 TableView :

# To both 64- and 32-bit processes: The registry view for 32-bit applications.
HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

# To 32-bit processes: same as above
# To 64-bit processes: the 64-bit view of the registry.
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*

换句话说:您的代码在 32 位进程中运行时,会执行两次相同的操作


仅使用注册表 key 路径将不允许 32 位进程查看 64 位注册表。

但是,有解决方法:

  • 通过目录 $env:WinDir\SysNative 将 PowerShell 的 64 位实例作为外部进程调用 - 参见下文。

  • 直接使用.NET类型,如this answer所示.

  • 调用标准reg.exe实用程序,它具有 /reg:32/reg:64 参数,用于针对特定位的配置单元。但是,使用这种方法您将失去 PowerShell 丰富的数据类型支持,并且您必须将结果解析为文本


使用脚本 block 从(32 位)PowerShell 调用 64 位 PowerShell { ... } 自动以 CLIXML 格式序列化 64 位实例的输出对象,这使得调用实例自动以合理的保真度反序列化它们[1],以便您的原始过滤命令应该起作用:

# Works only in a 32-bit process.
& $env:WINDIR\SysNative\WindowsPowerShell\v1.0\powershell.exe {
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*
} |
Where-Object {$_.DisplayName -like $appName} |
Select-Object @{Expression={$_.DisplayName + "|" + $_.DisplayVersion +"|x64"}} |
Sort-Object -Unique

要确定您的脚本是否在 32 位进程中运行,请使用以下命令:

# PowerShell v3 and above.
$is32Bit = [Environment]::Is64BitProcess

# PowerShell version 2 alternative
$is32Bit = ${env:ProgramFiles(x86)} -eq ${env:ProgramFiles} -or
-not ${env:ProgramFiles(x86)}

-or -not ${env:ProgramFiles(x86)} 部分检测运行的情况在纯 32 位 Windows 版本上,但请注意,显然,那里不存在 64 位定义。

在 PowerShell v3 及更高版本中,您可以使用 [Environment]::Is64BitOperatingSystem 轻松测试操作系统(Windows 安装)是否为 64 位

Theo's helpful answer提供一个辅助函数,以向后兼容的方式确定进程和操作系统位数。


[1] 除了一些众所周知的类型之外,输入对象被反序列化为 [PSCustomObject] 实例,其静态属性反射(reflect)原始对象的属性值 - 请参阅 this answer了解详情。

关于在 x86 上下文中运行时,PowerShell 返回已安装软件的不准确结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53687112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com