Consider the following powershell script:
Write-Host "Foo"
if($true)
{
Write-Host "Bar"
throw ("the matrix has a bug")
}
File saved to c:\test.ps1
This is the invocation, in a cmd window with result:
C:\>powershell.exe -NonInteractive -Command - <c:\test.ps1
Foo
C:\>
However, if I add 2 linebreaks after the }
the result is as expected:
Write-Host "Foo"
if($true)
{
Write-Host "Bar"
throw ("the matrix has a bug")
}
<linebreak here>
<linebreak here>
Same invocation, and THIS is the result:
C:\>powershell.exe -NonInteractive -Command - <c:\test.ps1
Foo
Bar
the matrix has a bug
At line:4 char:7
+ throw <<<< ("the matrix has a bug")
+ CategoryInfo : OperationStopped: (the matrix has a bug:String)
[], RuntimeException
+ FullyQualifiedErrorId : the matrix has a bug
C:\>
As expected
It appears the the <
in the command line writes line per line to the console of powershell. So thought, well, it might be that the CMD doesn't read all lines, but I wrote a small C# app that reads the console and prints it, and it reads the lines just fine.
This is the C# code:
using System;
namespace PrintArgs
{
internal class Program
{
private static void Main(string[] args)
{
foreach (string arg in args)
{
Console.WriteLine("----- ARG start");
Console.WriteLine(arg);
Console.WriteLine("----- ARG stop");
}
string line;
do
{
line = Console.ReadLine();
if (line != null)
{
Console.WriteLine(line);
}
}
while (line != null);
}
}
}
Invoking this (so basicly putting PrintArgs.exe on my C drive, and invoking that with the identical commands, with the test.ps1 with NO linebreaks) yields this:
C:\>printargs.exe -NonInteractive -Command - <c:\test.ps1
----- ARG start
-NonInteractive
----- ARG stop
----- ARG start
-Command
----- ARG stop
----- ARG start
-
----- ARG stop
Write-Host "Foo"
if($true)
{
Write-Host "Bar"
throw ("the matrix has a bug")
}
C:\>
Can somebody help me on this? (Sorry for the long post).