【问题标题】:How to remove a program through Powershell?如何通过 Powershell 删除程序?
【发布时间】:2020-05-19 05:19:09
【问题描述】:

我想创建一个函数来通过 powershell 删除程序。我似乎无法弄清楚到底该怎么做。

我的代码:

function RemoveProgram {
    $app = Get-CimInstance -Class Win32_Product | Where-Object {
        $_.Name -match $args
    }
    $app.Uninstall()
}

但是,当我运行它时,我得到了

Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named 'Uninstall'.

有没有人有一些关于如何让它变得更好的提示?

【问题讨论】:

  • CIM cmdlet没有与数据建立实时连接。这是设计使然。您需要使用Invoke-CimMethod 调用该方法或切换回使用Get-WmiObject,因为它仍然与数据有实时连接。
  • Get-WmIObject 在我的 powershell 版本上不起作用
  • 啊!那么您可能会将该信息 - 以及您的 PoSh 版本 - 放在您的问题中。这很重要... [咧嘴笑]

标签: function powershell scripting


【解决方案1】:

此代码将起作用。不过可能需要一段时间才能运行。

param(
    [string] $programToUninstall = "BlueJeans"    
)

function RemoveProgram([string] $program) {
    Invoke-CimMethod `
        -Query ('select * from Win32_Product where name like "%' + $program + '%"') `
        -MethodName "Uninstall" 
}

RemoveProgram -program $programToUninstall

【讨论】:

  • 您还可以通过管道将 get-ciminstance 传递给 invoke-cimmethod。
【解决方案2】:

仅安装 Powershell 5 和 msi。 Win32_Product 也只是 msi,并在运行时验证每个 msi,这就是它如此慢的原因。

function RemoveProgram {
  get-package *$args* | uninstall-package
}

【讨论】:

  • @thewindowsnoob 也许以后。目前 PS 7 不支持 msi 或程序提供程序。
【解决方案3】:

所以我实际上能够找到一种方法来做到这一点,而无需创建自己的函数。我使用了一个名为ProgramManagement 的模块。 https://github.com/pldmgg/ProgramManagement

希望这会有所帮助,并感谢任何试图帮助我解决此问题的人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 2019-04-23
    • 2018-02-12
    • 1970-01-01
    • 2018-04-19
    相关资源
    最近更新 更多