在使用PowerShell-Command -
可执行文件选项时,我遇到了以下限制:
Write-Host "SETUP"
Set-PSDebug -Trace 2
Write-Host "TEST ONE"
$foo = "BAR"
Write-Host "TEST TWO"
Get-ChildItem
Write-Host "TEST THREE"
$bar = @"
BAZ
"@
Write-Host "DONE"
如果我将之前的内容保存到test.ps1
并执行cat .\test.ps1 | powershell -Command -
,我会得到以下输出:
SETUP
DEBUG: 1+ >>>> Write-Host "TEST ONE"
DEBUG: ! CALL function '<ScriptBlock>'
TEST ONE
DEBUG: 1+ >>>> $foo = "BAR"
DEBUG: ! CALL function '<ScriptBlock>'
DEBUG: ! SET $foo = 'BAR'.
DEBUG: 1+ >>>> Write-Host "TEST TWO"
DEBUG: ! CALL function '<ScriptBlock>'
TEST TWO
DEBUG: 1+ >>>> Get-ChildItem
DEBUG: ! CALL function '<ScriptBlock>'
Directory: C:\empty
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 26/02/2017 15:39 179 test.ps1
DEBUG: 1+ >>>> Write-Host "TEST THREE"
DEBUG: ! CALL function '<ScriptBlock>'
TEST THREE
如您所见,该Write-Host "DONE"
行的输出永远不会显示。
因此,在逐行评估时,这里的字符串似乎发生了一些事情。为什么会这样?
而且,更重要的是,有没有办法让这里的字符串在这种情况下工作?