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

Fat*_*Hit 2 powershell start-process scriptblock powershell-core powershell-7.0

为什么Start-Process powershell.exe { Read-Host }有效,但Start-Process pwsh.exe { Read-Host }不起作用?

我知道 的-ArgumentList切换Start-Process,但是当涉及转义引号时,它使事情变得更加复杂:

PowerShell 7.0.3 和 7.2.0-preview.3:

Start-Process pwsh.exe -ArgumentList '-Command "& {
    ''Hello World.''
    Read-Host
}"' -Verb RunAs
Run Code Online (Sandbox Code Playgroud)

PowerShell 5.1:

Start-Process powershell.exe { 
    'Hello World.'
    Read-Host
} -Verb RunAs
Run Code Online (Sandbox Code Playgroud)

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

pwsh.exe {
    'Hello World.'
    Read-Host
}
Run Code Online (Sandbox Code Playgroud)

我在这里遗漏了一些东西还是有更好/不同的方法允许脚本块与脚本的其余部分并行执行?

gro*_*ser 6

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 ..)
Run Code Online (Sandbox Code Playgroud)

这意味着如果您想运行某些命令,则必须-Command { ... }在参数中写入。

但是您可以编写更短的命令来运行 pwsh,如下所示:

Start-Process pwsh.exe '-c', {
    'Hello World.'
    Read-Host
}
Run Code Online (Sandbox Code Playgroud)
  1. -c更短等价于-Command
  2. 脚本块将转换为参数中的有效字符串


js2*_*010 5

您不显示错误消息。Pwsh 默认为 -file,而 powershell 默认为 -command。从cmd运行:

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
Run Code Online (Sandbox Code Playgroud)

使用 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.
Run Code Online (Sandbox Code Playgroud)

“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>
Run Code Online (Sandbox Code Playgroud)

好像是自动运行的吧?

pwsh -encodedCommand IAAnAGgAaQAgAHQAaABlAHIAZQAnACAA -inputFormat xml -outputFormat text
Run Code Online (Sandbox Code Playgroud)