为什么 pwsh.exe 与 Start-Process 结合使用不直接接受脚本块?

Why does pwsh.exe in combination with Start-Process not accept a script block directly?

为什么 Start-Process powershell.exe { Read-Host } 有效,而 Start-Process pwsh.exe { Read-Host } 无效?

我知道 Start-Process-ArgumentList 开关,但在转义引号时它使事情变得更加复杂:

PowerShell 7.0.3 和 7.2.0-preview.3:

Start-Process pwsh.exe -ArgumentList '-Command "& {
    ''Hello World.''
    Read-Host
}"' -Verb RunAs

PowerShell 5.1:

Start-Process powershell.exe { 
    'Hello World.'
    Read-Host
} -Verb RunAs

另一方面,在创建新的 PowerShell 会话时(没有 Start-Process),仍然可以使用脚本块:

pwsh.exe {
    'Hello World.'
    Read-Host
}

我是不是遗漏了什么,或者是否有 better/different 方法允许脚本块与脚本的其余部分并行执行?

你没有显示错误信息。 Pwsh 默认为 -file 而 powershell 默认为 -command。 运行 来自命令:

pwsh { 'hi there' }
The argument '{' is not recognized as the name of a script file. Check the spelling of 
the name, or if a path was included, verify that the path is correct and try again.

pwsh -command { 'hi there' } 
hi there

使用 start-process -nonewwindow 可以看到错误:

Start-Process -NoNewWindow pwsh.exe { Read-Host }

The argument 'Read-Host' is not recognized as the name of a script file. Check the
spelling of the name, or if a path was included, verify that the path is correct and try 
again.

Usage: pwsh[.exe] [-Login] [[-File] <filePath> [args]]
                  [-Command { - | <script-block> [-args <arg-array>]
                                | <string> [<CommandParameters>] } ]
                  [-ConfigurationName <string>] [-CustomPipeName <string>]
                  [-EncodedCommand <Base64EncodedCommand>]
                  [-ExecutionPolicy <ExecutionPolicy>] [-InputFormat {Text | XML}]
                  [-Interactive] [-MTA] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile]
                  [-OutputFormat {Text | XML}] [-SettingsFile <filePath>] [-SSHServerMode]
[-STA]
                  [-Version] [-WindowStyle <style>] [-WorkingDirectory <directoryPath>]

       pwsh[.exe] -h | -Help | -? | /?

PowerShell Online Help https://aka.ms/powershell-docs

All parameters are case-insensitive.

"pwsh { 'hi there' } 在 powershell 中工作。我不确定为什么。

.\echoargs { 'hi there' }

Arg 0 is <-encodedCommand>
Arg 1 is <IAAnAGgAaQAgAHQAaABlAHIAZQAnACAA>
Arg 2 is <-inputFormat>
Arg 3 is <xml>
Arg 4 is <-outputFormat>
Arg 5 is <text>

好像自动运行这个?

pwsh -encodedCommand IAAnAGgAaQAgAHQAaABlAHIAZQAnACAA -inputFormat xml -outputFormat text

pwsh 在参数中默认获取文件名。
如果您尝试在 CMD 中 运行 它,您将看到:

CMD> pwsh { 'Hello!' }
The argument '{' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Usage: pwsh[.exe] [-Login] [[-File] <filePath> [args]]
                  [-Command { - | <script-block> [-args <arg-array>]
                                | <string> [<CommandParameters>] } ]
(.. other help from pwsh ..)

这意味着如果你想运行一些命令你必须在参数中写-Command { ... }

但是您可以像这样为 运行 pwsh 编写更短的命令:

Start-Process pwsh.exe '-c', {
    'Hello World.'
    Read-Host
}
  1. -c-Command
  2. 更短
  3. 脚本块 将转换为参数中的有效字符串

这是一个使用 start-process (powershell 7)

将脚本块发送到新 pwsh window 的工作示例
while(1)
{
    $script ={ write-host 'Hello World.' 
    pause
    }
    start-process -Filepath pwsh.exe -ArgumentList "-Command $script"
    pause
}

这会在来自

的新 window 中启动一个 ScriptBlock
Start-Process pwsh.exe -ArgumentList "-c", {
    'Hello World.'
    Read-Host
}, "-noexit" -WindowStyle Maximized

以下是从打开的 PowerShell 启动的脚本 window

Start-Process pwsh.exe -ArgumentList "-c", ".\Child-Script.ps1", "-noexit" -WindowStyle Maximized

这会在新的 window

中与父 .ps1 脚本在同一目录中启动子 .ps1

Parent-Script.ps1

Start-Process pwsh.exe -ArgumentList "-c", ("$PSScriptRoot\Child-Script.ps1"), "-noexit" -WindowStyle Maximized