【问题标题】:Uninstalling an application via uninstallString using powershell in WPF application在 WPF 应用程序中使用 powershell 通过 uninstallString 卸载应用程序
【发布时间】:2020-07-01 10:48:52
【问题描述】:

我正在尝试在 WPF 应用程序中使用以下 Powershell 脚本卸载软件

get-package |where name -like "Notepad++ (64-bit x64)" |% { & $_.Meta.Attributes["UninstallString"] /S}

上述命令仅适用于 Notepad++ (64-bit x64),但当我尝试使用 Git 版本 2.25.1TortoiseGit 2.10 等软件时失败。 0.0(64 位)

对于 Git 版本 2.25.1

get-package |where name -like "Git version 2.25.1" |% { & $_.Meta.Attributes["UninstallString"] /S} 

我收到以下错误:

&:无法识别术语“C:\Program Files\Git\unins001.exe” 作为 cmdlet、函数、脚本文件或可运行程序的名称。 检查名称的拼写,或者如果包含路径,请验证 路径正确,然后重试。在行:1 字符:59 + ... "Git 版本 2.25.1" |% { & $_.Meta.Attributes["UninstallString"] /S} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: ("C:\Program Files\Git\unins001.exe":String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

对于 TortoiseGit 2.10.0.0(64 位)

 get-package |where name -like "TortoiseGit 2.10.0.0 (64 bit)" |% { & $_.Meta.Attributes["UninstallString"] /S} 

我遇到以下错误:

管道元素中“&”之后的表达式产生了一个对象 无效。它必须产生一个命令名、一个脚本块或一个 命令信息对象。在行:1 字符:70 + ... t 2.10.0.0(64 位)" |% { & $_.Meta.Attributes["UninstallString"] /S} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : BadExpression

我也试过用WMI objects powershell script 下面是脚本

Get-WmiObject -Class Win32_Product | Where {$_.Name -like \"{returnStr}\"}  | foreach {$_.Uninstall()}".Replace("{returnStr}", Notepad++ (64-bit x64))   

以上脚本仅适用于通过 MSI 安装的软件

如果有人对如何继续提出建议,将不胜感激!

【问题讨论】:

    标签: c# wpf powershell wmi


    【解决方案1】:

    你可以在这里找到你朋友的卸载包:

    get-package | where-object name -like "Notepad++ (64-bit x64)" | ForEach {uninstall-package}
    

    【讨论】:

    • 您好,我尝试使用上述脚本,但出现错误 -Uninstall-Package:无法使用指定的命名参数解析参数集。在 line:1 char:75 + ... ect name -like "Notepad++ (64-bit x64)" | ForEach {uninstall-package} + ~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Uninstall-Package], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.PackageManagement。 Cmdlets.UninstallPackage
    • 您好,我尝试使用命令:get-package | where-object name -like "Notepad++ (64-bit x64)" | ForEach {Uninstall-Package -Name $_.Name },我没有收到任何错误但是软件包仍然没有被卸载。
    • 尝试在管道之后删除 ForEach 和 Uninstall-Package。
    • 您好,尝试使用以下命令:get-package | where-object name -like "Notepad++ (64-bit x64)"| Uninstall-Package,同样的问题没有错误,并且软件包仍然处于卸载状态。另外,我注意到相同的命令适用于 TortoiseGit 2.10.0.0 (64 bit)
    【解决方案2】:

    你必须去掉双引号。用 $null 或没有第二个参数替换 "。 Uninstall-package 不适用于非 msi 安装。此外,这仅适用于 windows powershell 5.1,目前不适用于 powershell 7。 TortoiseGit 有卸载字符串吗?

    get-package "*Git version 2.25.1*" | 
      % { & ($_.Meta.Attributes["UninstallString"] -replace '"') /S} 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 2010-09-11
      • 2011-12-17
      • 2011-03-25
      • 1970-01-01
      相关资源
      最近更新 更多