考慮下面的PowerShell腳本:古怪的命令行的行爲
Write-Host "Foo"
if($true)
{
Write-Host "Bar"
throw ("the matrix has a bug")
}
文件保存到C:\ test.ps1
這是調用,在CMD窗口結果:
C:\>powershell.exe -NonInteractive -Command - <c:\test.ps1
Foo
C:\>
但是,如果我的}
後加2個換行如預期的結果:
Write-Host "Foo"
if($true)
{
Write-Host "Bar"
throw ("the matrix has a bug")
}
<linebreak here>
<linebreak here>
相同的調用,這是結果:
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:\>
正如預期
它出現的<
在命令行每行寫入線的powershell的控制檯。所以我認爲,可能CMD不會讀取所有行,但是我編寫了一個小型C#應用程序,它可以讀取控制檯並打印出來,並且它讀取的行很好。
這是C#代碼:
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);
}
}
}
調用此(所以basicly把PrintArgs.exe我的C驅動器上,並調用與所述相同的命令,與test.ps1與NO換行符)產生這樣的結果:
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:\>
有人可以幫助我嗎? (對不起,很長的文章)。
相關 - http://stackoverflow.com/a/9173172/526535 – manojlds
我剛剛遇到這個。很奇怪,很想知道爲什麼。 @Snake:我不確定你的c#測試應用程序的源代碼是否特別相關;我認爲這有損於手頭的問題。 – richvdh