ai didi

windows - 用powershell执行卸载字符串

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

我正在尝试制作用于卸载软件的Powershell脚本。
这是代码:

$software = Read-Host "Software you want to remove"
$paths = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
Get-ChildItem $paths |
Where-Object{ $_.GetValue('DisplayName') -match "$software" } |
ForEach-Object{
$uninstallString = $_.GetValue('UninstallString') + ' /quiet /norestart'
Write-Host $uninstallString
& "C:\Windows\SYSTEM32\cmd.exe" /c $uninstallString
}
它适用于卸载字符串,例如
MsiExec.exe /X{C22F57FC-4B20-3354-8626-382E3C710B38} /quiet /norestart
但是如果我想卸载像 winrar 之类的东西,它们具有类似卸载的字符串
C:\Program Files\WinRAR\uninstall.exe
我收到以下错误
cmd.exe : 'C:\Program' is not recognized as an internal or external command,
任何想法请如何使此脚本工作
问候

最佳答案

问题是您无条件地附加了/quiet /norestart
解决方案是测试整个卸载字符串是否指向可执行文件:

$isExeOnly = Test-Path -LiteralPath $uninstallString
基于此,您可以决定如何处理此类可执行文件:
  • 如果您只想在不带参数的情况下执行它们:
  • $uninstallString = $_.GetValue('UninstallString')
    $isExeOnly = Test-Path -LiteralPath $uninstallString
    if (-not $isExeOnly) { $uninstallString += ' /quiet /norestart' }
  • 如果您确实想传递参数/quiet /norestart-但是请注意,如果目标可执行文件不理解以下参数,它们可能会拒绝执行:
  • $uninstallString = $_.GetValue('UninstallString')
    $isExeOnly = Test-Path -LiteralPath $uninstallString
    if ($isExeOnly) { $uninstallString = "`"$uninstallString`"" }
    $uninstallString += ' /quiet /norestart'
    替代方法是根据可执行文件名( Split-Path -Leaf $uninstallString)特殊设置要传递的参数(选项),但这显然很麻烦并且无法全面执行。

    关于windows - 用powershell执行卸载字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64071377/

    24 4 0
    文章推荐: elasticsearch - 连接来自Kibana的多个ES集群
    文章推荐: javascript - 通过 Typescript 支持将一个项目中的捆绑代码包含到另一个项目中
    文章推荐: javascript - 为什么不在类外部/顶部声明变量以避免使用此关键字?
    文章推荐: jquery - 在 chrome 扩展上保持相同的值
    行者123
    个人简介

    我是一名优秀的程序员,十分优秀!

    滴滴打车优惠券免费领取
    滴滴打车优惠券
    全站热门文章
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com