通过注册表读取卸载字符串静默卸载Brave浏览器报错如何解决
问题根因
- 注册表读取的
UninstallString值本身同时包含可执行文件路径和启动参数,你直接将完整字符串传入Start-Process的-FilePath参数,系统会将完整字符串识别为单个文件路径,自然找不到对应文件。比如Brave的卸载串末尾的--uninstall --system-level不属于路径部分,串内多余的双引号也没有做处理。 - 匹配逻辑混用了正则和通配符语法:
-match运算符遵循正则规则,你写的^*$appName^*属于无效正则,会导致匹配不到正确的卸载项。 - Brave的静默卸载参数不是通用的
/S,强行传该参数会导致静默卸载失效。
修正方案
核心逻辑是先把UninstallString拆分为文件路径和自带参数两部分,处理多余符号后补对对应软件的静默参数即可,该逻辑兼容你之前正常使用的其他软件卸载场景,无需单独适配。
修正后的代码如下:
$appName = "Brave" $uninstallInfo = $null # 按优先级读取32位、64位注册表卸载项 $regPaths = @( 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*', 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' ) foreach ($path in $regPaths) { $uninstallInfo = Get-ItemProperty $path | Where-Object { $_.DisplayName -like "*$appName*" } | Select-Object DisplayName, UninstallString -First 1 if ($uninstallInfo) { break } } if (-not $uninstallInfo) { Write-Host "未找到对应应用的卸载信息" exit 1 } $uninstallStr = $uninstallInfo.UninstallString $filePath = "" $arguments = @() # 拆分卸载串的执行路径和自带参数 if ($uninstallStr.StartsWith('"')) { $quoteEndIndex = $uninstallStr.IndexOf('"', 1) $filePath = $uninstallStr.Substring(1, $quoteEndIndex - 1) $remaining = $uninstallStr.Substring($quoteEndIndex + 1).Trim() if ($remaining) { $arguments += $remaining -split ' ' } } else { $spaceIndex = $uninstallStr.IndexOf(' ') if ($spaceIndex -eq -1) { $filePath = $uninstallStr } else { $filePath = $uninstallStr.Substring(0, $spaceIndex) $remaining = $uninstallStr.Substring($spaceIndex + 1).Trim() if ($remaining) { $arguments += $remaining -split ' ' } } } # 追加Brave专属静默卸载参数 $arguments += "--force-uninstall" # 执行卸载 Start-Process -Wait -FilePath $filePath -ArgumentList $arguments -PassThru
内容的提问来源于stack exchange,提问作者John418
相关产品推荐
相关产品推荐

