小编gbr*_*aad的帖子

Golang 调用 PowerShell.exe 总是返回 ASCII 字符

我正在使用 Go 编写的应用程序中的 PowerShell,但无法让它返回非 ASCII 字符。起初我使用 go-powershell,但遇到同样的问题:https : //github.com/gorillalabs/go-powershell/issues/10,现在使用稍微更基本的方法:

package main

import (
        "bytes"
        "fmt"
        "os/exec"
)

type PowerShell struct {
        powerShell string
}

func New() *PowerShell {
        ps, _ := exec.LookPath("powershell.exe")
        return &PowerShell{
                powerShell: ps,
        }
}

func (p *PowerShell) Execute(args ...string) (stdOut string, stdErr string, err error) {
        args = append([]string{"-NoProfile", "-NonInteractive"}, args...)
        cmd := exec.Command(p.powerShell, args...)

        var stdout bytes.Buffer
        var stderr bytes.Buffer
        cmd.Stdout = &stdout
        cmd.Stderr = &stderr

        err = cmd.Run()
        stdOut, stdErr = stdout.String(), …
Run Code Online (Sandbox Code Playgroud)

powershell go

3
推荐指数
1
解决办法
4948
查看次数

标签 统计

go ×1

powershell ×1