【问题标题】:Uninstalling an application by its GUID按 GUID 卸载应用程序
【发布时间】:2016-01-13 07:57:08
【问题描述】:

您好,我尝试使用 GUID 卸载产品,当我直接在命令提示符下执行它时效果很好,但是当我尝试使用 Golang 执行它时收到错误消息

我的代码:

// Powershell_Command
package main

import (
    "fmt"
    "os/exec"
)

func main() {
    out, err := exec.Command("cmd","/C","wmic","product","where","IdentifyingNumber=\"{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\"","call","uninstall").Output()
    fmt.Println("err::",err)
    fmt.Println("out::",string(out))
}

输出是:

err:: 退出状态 2147749911

输出::

提前致谢

【问题讨论】:

  • 这个问题和powershell有什么关系?

标签: go uninstallation wmic windows-installer


【解决方案1】:

(这个问题大部分与 Go 无关。)

有几点需要注意:

  1. 不要打电话给cmd.exe:它是用来运行脚本的,你不是在运行脚本而只是调用程序。所以你的电话变成了

     out, err := exec.Command("wmic.exe", "product", "where",
          `IdentifyingNumber="{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"`,
          "call", "uninstall").Output()
    

    (注意使用反引号来生成“原始”字符串——这有助于防止“反斜杠”。

  2. 你没有抓取你正在运行的程序的标准错误流。

    考虑使用exec.Cmd 类型的CombinedOutput()

    另外一点:除非您的 Go 程序属于“GUI”子系统(即,不打算在控制台窗口中运行),否则通常更明智的做法是让生成的程序将其输出到与其相同的媒体输出主机进程。为此,您只需将其标准流连接到您的流程的标准流:

    cmd := exec.Command("foo.exe", ...)
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Run()
    
  3. 您也不需要wmic — 只需直接呼叫msiexec

    msiexec.exe /uninstall {GUID}
    

    原因是wmic 最终会调用msiexec,因为除了调用卸载程序之外没有其他方法可以卸载 Windows 应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 2011-05-21
    • 2017-08-09
    相关资源
    最近更新 更多