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



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

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

PowerShell 7.0.3和7.2.0-预览。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
}

我是遗漏了什么,还是有更好/不同的方法允许脚本块与脚本的其余部分并行执行?

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. 脚本块将转换为参数中的有效字符串

您不会显示错误消息。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

通过启动进程-nonewwindows,您可以看到错误:

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在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

下面是一个使用启动进程(powershell 7(将脚本块发送到新pwsh窗口的工作示例

while(1)
{
$script ={ write-host 'Hello World.' 
pause
}
start-process -Filepath pwsh.exe -ArgumentList "-Command $script"
pause
}

这将在一个新窗口中启动一个ScriptBlock,它来自groser的注释

Start-Process pwsh.exe -ArgumentList "-c", {
'Hello World.'
Read-Host
}, "-noexit" -WindowStyle Maximized

以下启动是来自打开的PowerShell窗口的脚本

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

这将在新窗口中启动与父.ps1脚本相同目录中的子.ps1

父脚本.ps1

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

相关内容

  • 没有找到相关文章

最新更新