考虑以下 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);
}
}
}
调用它(所以基本上将 PrintArgs.exe 放在我的 C 驱动器上,并使用相同的命令调用它,使用没有换行符的 test.ps1 )会产生以下结果:
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:\>
有人可以帮我吗?(对不起,很长的帖子)。