douba1214
2013-12-03 09:41
浏览 251
已采纳

在Go中执行vbscript

I am trying to have a Go program execute a vbscript that adds several registry values. The Go code that handles this is as follows:

err = exec.Command("cmd.exe", "/c", "registry.vbs").Run()
if err != nil {
    fmt.Printf("Error: %s
", err.Error())
}

When I run my Go program and it gets to the part where it executes this vbscript absolutely nothing happens. The registry values are not changed and there are no errors. If I try to run the following command it works just fine:

cmd.exe /c C:\path\to\fileegistry.vbs

Things I have tried:

  • Add file path in the Go program
  • Run the Go program as an administrator
  • I tried using .Output() instead of .Run() and that resulted in the output equal to [ ]

Does anybody have any idea why this is happening?

Any direction would be greatly appreciated.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试让Go程序执行添加多个注册表值的vbscript。 处理此问题的Go代码如下:

  err = exec.Command(“ cmd.exe”,“ / c”,“ registry.vbs”)。Run(  )
if err!= nil {
 fmt.Printf(“ Error:%s 
”,err.Error())
} 
   
 
 

何时 我运行我的Go程序,它到达执行此vbscript的那一部分,绝对没有任何反应。 注册表值不会更改,并且没有错误。 如果我尝试运行以下命令,则可以正常运行:

  cmd.exe / c C:\ path \ to \ file 
egistry.vbs 
    
 
 

我尝试过的方法:

  • 在Go程序中添加文件路径
  • 运行 我以管理员身份进入程序
  • 我尝试使用 .Output()代替 .Run(),结果输出等于< code> []

    有人知道为什么会这样吗?

    任何方向都是 非常感谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duan19780629 2013-12-03 10:32
    已采纳

    There are so many things that can go wrong in your scenario, that you should start simple:

    (1) given a .vbs that does not try to change the registry (which Windows eagerly defends):

    MsgBox "ThatsMe"
    

    and the invocation

    err := exec.Command("cmd.exe", "/c", "ThatMe.vbs").Run()
    

    from an .exe in the same folder, I get a security alert: "Do you really want to open ThatsMe.vbs from the mapped network drive E:\". If I agree, the script is executed and the MsgBox appears. Your security settings may be so strict that you aren't even be asked.

    (2) For the above invocation to work, the shell must know how to handle .VBS files. Your assoc/ftype settings may not provide this info. Then

    err := exec.Command("wscript.exe", "ThatsMe.vbs").Run()
    

    or

    err := exec.Command("cscript.exe", "ThatsMe.vbs").Run()
    

    should work - interestingly without the security warning.

    (3) Instead of relying on the PATH and having/doing all files/work in the same folder, provinding full file specifications might be a good idea:

    err := exec.Command(
        "C:/WINDOWS/system32/wscript.exe",
        "E:/trials/SoTrials/answers/10024850/go/ThatsMe.vbs").Run()
    

    (4) If you can execute the humble ThatsMe.vbs, but your registry.vbs still fails, then you have to research who is allowed to see/change the parts of the registry you are interested in. Perhaps you have to invoke your executable as Administrator.

    (5) While experimenting, I got fairly decent error messages from Go for the (un)intended nasty things I tried (using %comspec% instead of cmd.exe, bad file specs, ...). But trying to read a non-existing registry item caused a Windows Script Host error popup and no Go error. So your "absolutely nothing happens" diagnosis makes me wonder, whether Windows hides error messages from you. There are dark options like "Display a notification about every script error" in the IExplorer Advanced settings.

    已采纳该答案
    打赏 评论
  • douli6605 2013-12-03 09:57

    Try using Cscript.exe instead of cmd.exe.

    Cscript.exe is a command-line version of the Windows Script Host that provides command-line options for setting script properties.

    To get the output, use Output() instead of Run().

    Full example:

    package main
    
    import (
        "fmt"
        "os/exec"
    )
    
    func main() {
        if out, err := exec.Command("Cscript.exe", "registry.vbs").Output(); err != nil {
            fmt.Printf("Error: %s
    ", err)
        } else {
            fmt.Printf("%s
    ", out)
        }
    }
    

    See "To run scripts using the command-line-based script host (Cscript.exe)" at TechNet.

    打赏 评论
  • dsc71976 2013-12-03 10:22

    Another approach is to make Windows run the default application for your .vbs file. Typically, that will be wscript.exe.

    打赏 评论

相关推荐 更多相似问题