【问题标题】:Output from external exe and my custom objects in powershell来自外部 exe 的输出和我在 powershell 中的自定义对象
【发布时间】:2010-05-06 10:09:01
【问题描述】:

(对不起,奇怪的标题,没有想出更好的东西..)

背景

我使用 nunit-console 来测试我的程序集。它是这样调用的(简化):

function Test-ByNunit {
    param($assembly, $tempFile = 'c:\temp\nunit.xml')
    & <path-to-nunit-console> $assembly /nologo /xml:$tempFile @othparam 
}
Test-ByNunit c:\temp\myAssembly.dll

我没有问题,它工作正常。

问题

nunit-console 应该输出它的消息。这意味着 - 如果没有捕获,它应该将它们发送到屏幕,否则它可以存储在文件中 (Test-ByNunit $dll | set-content path)

我想以PSObject 对象数组的形式返回有关已运行的每个测试用例的信息(信息存储在 /xml 文件中)。

问题

您有任何提示如何返回信息并仍然让 nunit 输出其消息吗?
如果我只是将其写入输出,该函数将返回字符串数组(从 nunit-console 输出)和我的对象数组。然后重定向到输出文件也将存储我的对象,但我只想在控制台窗口中显示它们。

唯一可行的方法是使用[ref],但我想避免它。

(这不仅仅是关于 nunit-console,当然这是一个普遍的问题)

【问题讨论】:

    标签: powershell console nunit-console capture-output


    【解决方案1】:

    如果我完成了任务,那么Out-Host 应该会有所帮助:

    function Get-WithOutHost {
        # external output is redirected to the host
        cmd /c dir | Out-Host
        # normal output to be reused later
        Get-Process
    }
    
    # call    
    $result = Get-WithOutHost
    
    # now $result holds the data to use, external output is on the screen
    

    编辑:如果外部输出也应该被重用,而不仅仅是显示,这当然是不够的

    【讨论】:

    • 嗯,似乎我错过了一些基本的东西;)我想没有更好的答案了。
    猜你喜欢
    • 2016-01-11
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    相关资源
    最近更新 更多