When playing with the -Command -
option of the PowerShell executable, I came across the following limitation:
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"
If I save the previous content to test.ps1
and execute cat .\test.ps1 | powershell -Command -
, I get the following output:
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
As you can see, the output from the Write-Host "DONE"
line never shows.
So, there seems to be something going on for here strings when evaluating line by line. Why is this happening?
And, more importantly, is there a way to make here strings work in this context?