Reputation: 613
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
Upvotes: 0
Views: 1994
Reputation: 55563
(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 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".
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()
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.
Upvotes: 2