检测Local AppData中Google Chrome安装状态的脚本异常排查
问题:Chrome用户级安装检测脚本无法找到注册表项
我编写了一个脚本,用于检测Google Chrome是否安装在用户的Local AppData文件夹中,检测逻辑为检查HKEY_USERS\UserSID\Software\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome注册表项是否存在。
在测试虚拟机中,我使用两个测试用户安装了Google Chrome,手动查看上述注册表路径时能看到Google Chrome文件夹,但运行以下脚本时却提示找不到该注册表项,请问我遗漏了什么?
$registryPath = "HKU:\*\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome" $outputFile = "\\FileShare\NewFolder\Chrome.txt" if (Test-Path $registryPath) { $InstallLocation = (Get-ItemProperty -Path $registryPath -Name InstallLocation).InstallLocation $displayVersion = (Get-ItemProperty -Path $registryPath -Name DisplayVersion).DisplayVersion $output = "$($env:COMPUTERNAME), $InstallLocation, $displayVersion, $(Get-Date -Format ""dd-MM-yyyy HH:mm:ss"")" $output | Out-File -Append -FilePath $outputFile Write-Host "Checking ready." } else { #Registry key not found Write-Error "Can't find the registry" }
问题分析与解决方案
以下是你遗漏的关键问题及修正方案:
1. 通配符*在Test-Path中不生效
Test-Path不支持在注册表路径中用*匹配多个用户SID,直接写HKU:\*\...会被当作字面路径处理,自然无法找到对应项。必须先枚举HKU下的所有用户SID,再逐个检查目标路径。
2. 未考虑32位/64位注册表视图差异
如果是64位系统上安装了32位Chrome,注册表项会被重定向到SOFTWARE\WOW6432Node路径下,需要同时检查这个位置。
3. 未登录用户的注册表Hive未加载
只有当前登录的用户,其注册表Hive(NTUSER.DAT)会被加载到HKU中;若测试用户未登录,对应的SID路径不会出现在HKU下——你手动查看时可能处于用户登录状态,但脚本运行时该Hive未加载,导致找不到项。
4. 权限不足
运行脚本的账户可能没有读取其他用户注册表项的权限,需确保账户拥有访问HKU下其他用户SID路径的权限。
修正后的脚本示例
$outputFile = "\\FileShare\NewFolder\Chrome.txt" $date = Get-Date -Format "dd-MM-yyyy HH:mm:ss" # 枚举HKU下的所有用户SID $userSIDs = Get-ChildItem -Path HKU:\ | Where-Object { $_.Name -match 'S-\d-\d+-(\d+-){1,14}\d+' } foreach ($sid in $userSIDs) { # 同时检查常规路径和32位路径 $paths = @( "$($sid.PSPath)\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome", "$($sid.PSPath)\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome" ) foreach ($path in $paths) { if (Test-Path $path) { $regProps = Get-ItemProperty -Path $path -Name InstallLocation, DisplayVersion -ErrorAction SilentlyContinue if ($regProps) { $output = "$($env:COMPUTERNAME), $($regProps.InstallLocation), $($regProps.DisplayVersion), $date" $output | Out-File -Append -FilePath $outputFile Write-Host "Found Chrome installation for SID $($sid.PSChildName)" } } } } # 检查是否未找到任何安装记录 if (-not (Get-Content -Path $outputFile -ErrorAction SilentlyContinue | Where-Object { $_ -match $env:COMPUTERNAME })) { Write-Error "Can't find the registry" }
内容的提问来源于stack exchange,提问作者TomJD92
相关产品推荐
相关产品推荐

