dongtou2016 2016-01-13 07:57
浏览 236
已采纳

通过其GUID卸载应用程序

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条回答 默认 最新

  • doujiaozhao2489 2016-01-13 10:38
    关注

    (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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器