英文:
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无关。)
但是有几点需要注意:
-
不要调用
cmd.exe
:它是用于运行脚本的,而你不是在运行脚本,而只是调用一个程序。所以你的调用应该是:out, err := exec.Command("wmic.exe", "product", "where", `IdentifyingNumber="{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"`, "call", "uninstall").Output()
(注意使用反引号来创建一个“原始”字符串——这有助于防止“反斜杠问题”。
-
你没有获取正在运行的程序的标准错误流。
考虑使用
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()
-
你也不需要
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:
-
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 becomesout, 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".
-
You don't grab the standard error stream of the program you're running.
Consider using the
CombinedOutput()
of theexec.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()
-
You don't need
wmic
either—just call out tomsiexec
directly:msiexec.exe /uninstall {GUID}
The reason is that
wmic
would end up callingmsiexec
anyway because there's no other way to uninstall a Windows application other than calling its uninstaller.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论