通过其GUID卸载应用程序。

huangapple go评论81阅读模式
英文:

Uninstalling an application by its GUID

问题

你好,我尝试使用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:: exit status 2147749911
> 
> out::

谢谢提前帮助。

英文:

Hi I tried to uninstall a product using a GUID, it worked fine when I directly executed it in command prompt however, I get an error message when I try to execute it using Golang

My Code:

// 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))
}

the output is:

> err:: exit status 2147749911
>
> out::

Thanks in Advance

答案1

得分: 2

(这个问题大部分与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应用程序。

英文:

(This question for the most part has nothing to do with Go.)

A couple of things to note though:

  1. Don't call to cmd.exe: it's for running scripts, and you're not running a script but merely calling a program. So your call becomes

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

    (Notice the usage of the backquotes to make a "raw" string—this helps preventing "backslashity".

  2. You don't grab the standard error stream of the program you're running.

    Consider using the CombinedOutput() of the exec.Cmd type.

    One another point: unless your Go program is of "GUI" subsystem (that is, not intended to run in a console window) it's typically more sensible to just let the spawned program output whatever it outputs to the same media as its host process. To do this, you just connect its standard streams to those of your process:

     cmd := exec.Command("foo.exe", ...)
     cmd.Stdin = os.Stdin
     cmd.Stdout = os.Stdout
     cmd.Stderr = os.Stderr
     err := cmd.Run()
    
  3. You don't need wmic either—just call out to msiexec directly:

     msiexec.exe /uninstall {GUID}
    

    The reason is that wmic would end up calling msiexec anyway because there's no other way to uninstall a Windows application other than calling its uninstaller.

huangapple
  • 本文由 发表于 2016年1月13日 15:57:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/34761184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定