PowerShell脚本问题:筛选Adobe Flash Player卸载项执行卸载报错
问题分析与解决方案
错误原因
你的脚本报错是因为执行Select-Object -Property DisplayName, UninstallString后,输出的对象已经是仅包含指定属性的自定义PowerShell对象,不再是注册表项对象,因此调用$_.GetValue()方法会触发方法不存在的错误。
修正后的脚本
方案1:移除多余的Select-Object步骤
直接对注册表项对象进行筛选和处理,保留原始对象的方法和属性:
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object { $_.DisplayName -like "*Adobe Flash Player*" } | ForEach-Object { # 直接访问UninstallString属性 $uninstallString = $_.UninstallString # 处理卸载字符串,通常需要去掉引号并添加静默参数 $uninstallCommand = $uninstallString -replace '"', '' # 添加静默卸载参数(Flash Player的静默参数是 /s) cmd /c "$uninstallCommand /s" }
方案2:保留Select-Object,直接访问属性
如果需要保留属性筛选,直接通过对象属性访问,而非GetValue()方法:
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object { $_.DisplayName -match "Adobe" } | Select-Object -Property DisplayName, UninstallString | ForEach-Object { if ($_.DisplayName -like "*Adobe Flash Player*") { $uninstallString = $_.UninstallString $uninstallCommand = $uninstallString -replace '"', '' cmd /c "$uninstallCommand /s" } }
额外优化说明
- 添加
*通配符:用"*Adobe Flash Player*"匹配所有包含该名称的项,避免因版本后缀导致漏匹配 - 处理引号:卸载字符串通常包含双引号,需要移除后才能正确执行
- 静默参数:添加
/s实现无交互静默卸载,适合域内批量推送场景
内容的提问来源于stack exchange,提问作者JPrentice
相关产品推荐
相关产品推荐

