在新窗口中打开Powershell核心的正确方法是什么?

此命令将打开一个新的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进行这种调用的正确方法是什么?

wuchun198799 回答:在新窗口中打开Powershell核心的正确方法是什么?

请勿将脚本块({ ... })与Start-Process一起使用-它会将作为字符串绑定到-ArgumentList参数,这意味着其文字内容-除了封闭的{}-之外。

  • Windows PowerShell powershell.exe)中,CLI的默认参数为-Command

  • 在PowerShell Core 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的使用。

,

此外,如果您运行start-process -nonewwindow,则会看到错误消息“找不到文件”。运行这样的脚本应该可以:start-process pwsh .\script.ps1

本文链接:https://www.f2er.com/3134756.html

大家都在问