此命令打开一个新的 powershell 窗口,运行命令,然后退出:

Start-Process powershell { echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye" }

如果我改为启动 Powershell Core,它会打开一个新窗口,但新窗口会立即退出:
Start-Process pwsh { echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye" }

使用 pwsh 进行此类调用的正确方法是什么?

最佳答案

不要将脚本块 ( { ... } ) 与 Start-Process 一起使用 - 它作为字符串绑定(bind)到 -ArgumentList 参数,这意味着它的文字内容 - 除了封闭的 {} - 被传递。

  • 在 Windows PowerShell ( powershell.exe ) 中,CLI 的默认参数是 -Command
  • 在 PowerShell Core (v6+, pwsh.exe / pwsh ) 中,它是 -File [1],这就是您的命令失败的原因。

  • 因此,在 PowerShell Core 中,您必须明确使用 -Command ( -c ):
    Start-Process pwsh '-c', 'echo "hello"; sleep 1; echo "two"; sleep 1; echo "goodbye"'
    

    [1] 为了在类 Unix 平台上正确支持在 shebang lines 中使用 PowerShell Core,此更改是必要的。

    关于powershell - 在新窗口中打开powershell核心的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58774632/

    10-09 20:09
    查看更多