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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算