Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#控制台应用程序:写入PowerShell控制台而不发送到管道?_C#_Powershell - Fatal编程技术网

C#控制台应用程序:写入PowerShell控制台而不发送到管道?

C#控制台应用程序:写入PowerShell控制台而不发送到管道?,c#,powershell,C#,Powershell,我有以下C#.Net核心应用程序 class Program { static void Main(string[] args) { ???.WriteLine("yyy"); // Need to display "yyy" always even when running $a = .\test.exe in the powershell console Console.WriteLine("xxx"); // "xxx" can be sen

我有以下C#.Net核心应用程序

class Program
{
    static void Main(string[] args)
    {
        ???.WriteLine("yyy"); // Need to display "yyy" always even when running $a = .\test.exe in the powershell console
        Console.WriteLine("xxx"); // "xxx" can be sent to the PowerShell pipeline
    }
}
以下Powershell代码将“xxx”分配给
$a
。我想在控制台上显示“yyy”

$a = .\Test.exe # $a got "xxx". Nothing is displayed
$a = .\Test.exe # $a got Hello World. Nothing is displayed
$a # <== this line should write the value of $a to the console

如果我想在将最终结果分配给
$a
之前向控制台输出一些其他文本,该怎么办?

如果您在Powershell脚本中自己的行中放置$a,它将被写入控制台

$a = .\Test.exe # $a got "xxx". Nothing is displayed
$a = .\Test.exe # $a got Hello World. Nothing is displayed
$a # <== this line should write the value of $a to the console
$a=。\Test.exe#$a得到了Hello World。没有显示任何内容
$a#注意:以下内容适用于在控制台窗口(终端)中运行PowerShell,这是典型的,但请注意,也可以在非控制台应用程序中托管PowerShell

您唯一的选择是将不希望PowerShell通过其成功输出流捕获的文本写入stderr(标准错误流),而不是stdout(标准输出流):

  • Console.WriteLine()
    实际上与
    Console.Out.WriteLine()
    相同,即它写入标准输出,PowerShell将标准输出映射到它的成功输出流,这意味着可以在变量中捕获输出或通过管道进行进一步处理

  • 相比之下,
    Console.Error.WriteLine()
    会写入stderr,默认情况下,PowerShell会将其传递到控制台(显示器)(有关如何捕获它的信息,请参见下文)

请注意,也可以捕获stderr输出,即通过重定向
2>

  • 2>&1
    将stderr输出合并到PowerShell的成功输出流中,以便可以捕获stderr和stdout的输出;注意,通过stderr接收的行实际上被捕获为
    System.Management.Automation.ErrorRecord
    实例

  • 2>stderr.txt
    将stderr输出保存到文件
    stderr.txt

请注意,虽然常规控制台程序只有2个输出流-stdout和stderr-但PowerShell内部支持6个输出流,如[2]中所述



[2] 在系统级别,只有stdout和stderr作为进程间通信的输出流存在,因此PowerShell在从外部程序接收输出和向外部程序发送输出时必须将其内部流映射到这两个流。

您的意思是要记录一个未返回到PowerShell脚本的值?不太清楚$在Test.exe完成之前不会填充,因此
Console.WriteLine(“Hello World!”)将已经“在分配最终结果之前向控制台输出一些值”。在PowerShell中运行
$a=。\Test.exe
时,您能否明确说明您想要实现的目标。“在PowerShell中运行$a=。\Test.exe时,不显示任何内容”…抱歉,但在问题中,您说“以下PowerShell代码将获得“Hello World”。现在您说它没有。是哪个?@ADyson OP的意思是,可执行文件的输出被分配给变量,而不是写入主机控制台。我甚至不确定后者是否可行,因为主机控制台和输出流之间的区别是一个PowerShell概念,而可执行文件也应该在PowerShell之外工作。@ADyson我的理解是OP希望可执行文件将一些输出直接写入主机控制台,将其他输出写入StdOut(以便在从PowerShell调用命令时可以将其分配给变量)。但正如我所说,我甚至不确定这是否可能。我需要显示不同的值,从$a到控制台。问题是如何使
Test.exe
也写入在PowerShell中分配给变量时未捕获的内容。