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

gbr*_*aad 3 powershell go

我正在使用 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(), stderr.String()
        return
}

func main() {
        posh := New()
        stdout, stderr, err := posh.Execute("$OutputEncoding = [Console]::OutputEncoding; (Get-VMSwitch).Name")

        fmt.Println(stdout)
        fmt.Println(stderr)

        if err != nil {
                fmt.Println(err)
        }
}
Run Code Online (Sandbox Code Playgroud)

但同样的事情也会发生。而不是 gettting Prze???,它返回Przelas。这将导致在代码中进一步使用此虚拟交换机名称创建 VM 时出现问题。它不会被识别和错误。

注:$OutputEncoding = [Console]::OutputEncoding;没有任何影响。它确实发生了变化,但结果保持不变。

注意 2:直接从命令提示符调用相同的命令没有问题:powershell.exe -NoProfile -NonInteractive $OutputEncoding = [Console]::OutputEncoding; (Get-VMSwitch).Name")甚至powershell.exe -NoProfile -NonInteractive (Get-VMSwitch).Name"). 换句话说,它仅在使用 Go 时从 Go 执行此操作exec.Command

注意 3:这是为了解决虚拟机驱动程序在本地化名称方面的问题。是的,它可以使用 GUID ( .Id) 代替,但此问题在系统的不同部分仍然存在。

chu*_*ckx 5

伙计,Powershell 很有趣。这主要是一系列反复试验的结果。

基本上,您想设置[Console]::OutputEncoding而不是捕获它。

但是,为了自己清理,将其恢复为原始值并没有什么坏处。我还没有完全理解它,但通过多次exec()调用,这种变化仍然存在。

下面是一个例子:

<... everything else is as above ...>

func main() {
        posh := New()

        fmt.Println("With encoding change:")
        stdout, stderr, err := posh.Execute(
                "$test = \"Prze???\"\n" +
                "$old = [Console]::OutputEncoding\n" +
                "[Console]::OutputEncoding = [Text.Encoding]::UTF8\n" +
                "[Console]::OutputEncoding\n" +
                "$test\n" +
                "[Console]::OutputEncoding = $old")
        fmt.Println(stdout)
        fmt.Println(stderr)
        if err != nil {
                fmt.Println(err)
        }

        fmt.Println("Without encoding change:")
        stdout, stderr, err = posh.Execute(
                "$test = \"Prze???\"\n" +
                "[Console]::OutputEncoding\n" +
                "$test")
        fmt.Println(stdout)
        fmt.Println(stderr)
        if err != nil {
                fmt.Println(err)
        }
}
Run Code Online (Sandbox Code Playgroud)

输出:

$ ./exec-powershell.exe
With encoding change:


BodyName          : utf-8
EncodingName      : Unicode (UTF-8)
HeaderName        : utf-8
WebName           : utf-8
WindowsCodePage   : 1200
IsBrowserDisplay  : True
IsBrowserSave     : True
IsMailNewsDisplay : True
IsMailNewsSave    : True
IsSingleByte      : False
EncoderFallback   : System.Text.EncoderReplacementFallback
DecoderFallback   : System.Text.DecoderReplacementFallback
IsReadOnly        : False
CodePage          : 65001

Prze???




Without encoding change:


IsSingleByte      : True
BodyName          : IBM437
EncodingName      : OEM United States
HeaderName        : IBM437
WebName           : IBM437
WindowsCodePage   : 1252
IsBrowserDisplay  : False
IsBrowserSave     : False
IsMailNewsDisplay : False
IsMailNewsSave    : False
EncoderFallback   : System.Text.InternalEncoderBestFitFallback
DecoderFallback   : System.Text.InternalDecoderBestFitFallback
IsReadOnly        : True
CodePage          : 437

Przelas
Run Code Online (Sandbox Code Playgroud)