【问题标题】:Remove arguments from UninstallString从 UninstallString 中删除参数
【发布时间】:2018-11-20 12:22:52
【问题描述】:

我正在从 Chrome 卸载程序中提取卸载字符串,以便使用以下方式进行无人值守的静默卸载:

$UninstallStrings = Get-ItemProperty -Path "HKLM:\SOFTWARE\WoW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" |
    Where-Object{$_.DisplayName -like $ProgramName} |
    Select-Object -ExpandProperty UninstallString -ea SilentlyContinue

这给了我这个结果:

"C:\Program Files (x86)\Google\Chrome\Application\67.0.3396.79\Installer\setup.exe" --uninstall --system-level

现在要继续卸载,我想我需要从结果中删除参数,然后执行Start-Process。任何人都可以提示删除参数并将它们添加为-ArgumentsList 之后的正确方法吗?

【问题讨论】:

  • $UninstallStrings | Invoke-Expression
  • 收到错误“表达式或语句中出现意外的令牌‘卸载’。”使用 $UninstallStrings |调用表达式。我想去掉 --uninstall...... 位,并在脚本开头定义的 .exe 中添加一组不同的变量。
  • 试试$UninstallStrings -replace '^', '& ' | Invoke-Expression

标签: powershell uninstallation


【解决方案1】:

我能想到的获取安装路径的一种非常简单的方法是根据用于启动参数的特殊字符进行拆分,在本例中为“--”,然后选择第一个条目结果数组。

例如,如果你在这一点上:

$UninstallStrings = Get-ItemProperty -Path "HKLM:\SOFTWARE\WoW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" |
Where-Object{$_.DisplayName -like $ProgramName} |
Select-Object -ExpandProperty UninstallString -ea SilentlyContinue

然后你可以取那个变量,像这样拆分它:

$ExpandedUninstallString = $UninstallStrings -split "--"
$UninstallCommand = $ExpandedUninstallString[0]
$Arguments = $ExpandedUninstallString[1..($ExpandedUninstallString.Length)]

应该将 $UninstallCommand 设置为一个指向卸载程序的字符串,并设置该 $Arguments 数组中的现有参数。

另外请注意,这不能扩展到涵盖其他应用程序,除非您使用一些 if 语句对其进行定制,以检查安装字符串用作该特定应用程序的参数的字符,并将拆分分隔符定制到该特定参数列表。

【讨论】:

    【解决方案2】:
    $ProgramName = "Google Chrome"
    $EXEArgumente = "--uninstall --force-uninstall --system-level"
    $UninstallStrings = Get-ItemProperty -Path "HKLM:\SOFTWARE\WoW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object{$_.DisplayName -like $ProgramName} | Select-Object -ExpandProperty UninstallString -ea SilentlyContinue
    
    $ExpandedUninstallString = $UninstallStrings -split "--" -replace "`"",""
    $UninstallCommand = $ExpandedUninstallString[0]
    
    Start-Process $UninstallCommand -ArgumentList "$EXEArgumente"
    

    这就是我现在的工作方式。感谢大家的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2018-12-08
      相关资源
      最近更新 更多