3

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).

Anemoia
  • 7,928
  • 7
  • 46
  • 71
  • 2
    Relevant - http://stackoverflow.com/a/9173172/526535 – manojlds Sep 06 '12 at 09:36
  • I just came across this as well. Very odd, would love to know why. @Snake: I'm not sure the source for your c# test app is particularly relevant; I think it detracts from the question at hand. – richvdh Oct 26 '12 at 16:00

1 Answers1

1

it works, if you are omitting the "<" I/O redirect.

C:\>powershell.exe -NonInteractive c:\test.ps1
TimPe
  • 44
  • 3