Golang执行官.命令获取错误"退出状态0x80041017",该错误在CMD中成功运行



Go 中的代码

out, err := exec.Command("wmic", "computersystem", "where", "name="%computername%"", "call", "rename", "name", "newname").CombinedOutput()
if err != nil {
fmt.Println("err: ", err)
fmt.Println("out: ", string(out))
}

错误和输出(err:out:由我添加(:

err:  exit status 0x80041017
out:  Node - S
ERROR:        
Description = Invalid query

但直接在CMD中运行成功:

wmic computersystem where name="%computername%" rename newname
Executing (\SROOTCIMV2:Win32_ComputerSystem.Name="S")->rename()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 0;
};

os/exec文档中所述:

在Windows上,进程将整个命令行作为单个字符串接收,并进行自己的解析。命令使用与使用CommandLineToArgvW(这是最常见的方式(的应用程序兼容的算法,将Args组合并引用到命令行字符串中。值得注意的例外是msiexec.exe和cmd.exe(因此,所有批处理文件(,它们具有不同的无引号算法。在这些或其他类似的情况下,您可以自己进行引用,并在SysProcAttr.CmdLine中提供完整的命令行,使Args为空。

cmd := exec.Command("wmic")
cmdLine := `computersystem where name="$computername" rename ` + newName
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: "/c " + os.ExpandEnv(cmdLine)}
out, err := cmd.CombinedOutput() // Of course Run or Output

相关内容

最新更新