如何将 PowerShell 脚本嵌入到 GO 程序中以构建单个二进制文件

huangapple go评论93阅读模式
英文:

How to embed powershell script inside GO program to build a single binary

问题

我喜欢将一些 PowerShell 脚本嵌入到一个 Go 二进制文件中。我该如何嵌入并执行这些命令呢?
目前,我通过运行外部脚本来实现:

out,err := exec.Command("Powershell", "-file" , "C:\\test\\go\\my.ps1").Output()
if err != nil {
     log.Fatal(err)

}

fmt.Printf("来自 PowerShell 脚本的数据为:%s", out)

my.ps1 的内容为:

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name

我还有一些其他的 PowerShell 脚本,我想将它们添加到我的 Go 二进制文件中。我尝试了一些在互联网上找到的方法,但都失败了。

英文:

I like to embed some powershell scripts in one Go binary. How can I embed them and execute the commands from GO.
Currently I got it by running an external script:

out,err := exec.Command("Powershell", "-file" , "C:\\test\\go\\my.ps1").Output()
if err != nil {
     log.Fatal(err)

}

fmt.Printf("Data from powershell script is: %s", out)

my.ps1 content:

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name

I got a few more powershell scripts, I like to add the powershell scripts into my go binary file.
Have tryed a few things I did find on the internet but failed.

答案1

得分: 2

嵌入你的资源:

import (
    _ "embed"
)

//go:embed my.ps1
var shellBody []byte

然后,当你需要让主机操作系统访问它时,通过临时文件将其暴露出来:

tmp, err := ioutil.TempFile("/tmp", "powershell.*.ps1")
if err != nil {
    log.Fatal(err)
}
defer os.Remove(tmp.Name()) // 完成后清理

f, err := os.OpenFile(tmp.Name(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
    log.Fatal(err)
}

_, err = f.Write(shellBody)
if err != nil {
    log.Fatal(err)
}

err = f.Close()
if err != nil {
    log.Fatal(err)
}

然后你可以调用 Powershell

out, err := exec.Command("Powershell", "-file", tmp.Name()).Output()
if err != nil {
    log.Fatal(err)
}
英文:

Embed your asset:

import (
    _ "embed"
)

//go:embed my.ps1
var shellBody []byte

and then when you need the host OS to access this, surface it via a temp file:

tmp, err := ioutil.TempFile("/tmp", "powershell.*.ps1")
if err != nil {
    log.Fatal(err)
}
defer os.Remove(tmp.Name()) // clean-up when done


f, err := os.OpenFile(tmp.Name(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
    log.Fatal(err)
}

_, err = f.Write(shellBody)
if err != nil {
    log.Fatal(err)
}

err = f.Close()
if err != nil {
    log.Fatal(err)
}

then you can invoke Powershell:

out, err := exec.Command("Powershell", "-file" , tmp.Name()).Output()
if err != nil {
    log.Fatal(err)
}

答案2

得分: 1

你可以使用go1.16和embed,你应该创建一个单独的文件夹来保存嵌入文件的定义,然后使用另一个函数返回字符串或byte[]文件引用。

package main

import (
    "embed"
    "text/template"
)

var (
    // file.txt resides in a separated folder
    //go:embed file.txt
    myfile string
)

func main() {
    fmt.Printf("File %q\n", myfile)
}
英文:

You can use go1.16 and embed, you should create a separated folder to save the embed file definitions, then use another function to return the string or byte[] file reference.

package main

import (
    "embed"
    "text/template"
)

var (
    // file.txt resides in a separated folder
    //go:embed file.txt
    myfile string
)

func main() {
    fmt.Printf("File %q\n", myfile)
}

huangapple
  • 本文由 发表于 2021年11月30日 20:39:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/70169499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定