如何以与“cmd /c <command>”.execute() 相同的方式从 groovy 运行 PowerShell 命令?

WIL*_*MAN 6 windows powershell groovy

在 Groovy 中,我可以直接运行 Windows cmd shell 并像这样读取结果:

def proc = "cmd /c dir".execute()
proc.wait()
println "stdout: ${proc.in.text}"
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用 PowerShell 尝试此操作,它会阻止并且不会返回:

def proc = "powershell dir".execute()
Run Code Online (Sandbox Code Playgroud)

我试过了

def proc = "powershell -NonInteractive dir".execute()
Run Code Online (Sandbox Code Playgroud)

等等 - 但它们都阻塞了,我必须杀死 Groovy 脚本。

/c与 PowerShell 一起使用以获取返回到脚本的结果的 cmd 开关的等效项是什么。

Mal*_*alk 3

使用-命令参数:

powershell -command "dir"