有条件地管道到 Out-Null [英] Conditionally piping to Out-Null

查看:58
本文介绍了有条件地管道到 Out-Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 PowerShell 脚本来 msbuild 一堆解决方案.我想计算有多少解决方案成功构建,有多少失败.我也想查看编译器错误,但仅从第一个失败的错误开始(我假设其他人通常会有类似的错误,我不想弄乱我的输出).

我的问题是关于如何运行外部命令(在本例中为 msbuild),但有条件地通过管道传输其输出.如果我正在运行它并且还没有出现任何故障,我不想通过管道传输它的输出;我希望它直接输出到控制台,没有重定向,所以它会对其输出进行颜色编码.(与许多程序一样,如果 msbuild 发现其标准输出被重定向,则会关闭颜色编码.)但如果我之前遇到过失败,我想通过管道传输到 Out-Null.

显然我可以这样做:

if ($SolutionsWithErrors -eq 0) {msbuild $Path/nologo/v:q/consoleloggerparameters:ErrorsOnly} 别的 {msbuild $Path/nologo/v:q/consoleloggerparameters:ErrorsOnly |出空}

但似乎必须有一种方法可以避免重复.(好吧,它不必是重复的——如果我无论如何都将管道设置为 null,我可以省略 /consoleloggerparameters——但你明白了.)

可能有其他方法可以解决这个问题,但今天,我特别想知道:有没有办法运行命令,但只有在满足特定条件时才通过管道输出(否则不管道或重定向)它的输出,所以它可以做一些花哨的东西,比如颜色编码的输出)?

解决方案

您可以将输出命令定义为变量并使用 Out-DefaultOut-Null:

#根据条件设置输出命令$output = if ($SolutionsWithErrors -eq 0) {'Out-Default'} else {'Out-Null'}# 使用变量输出调用命令msbuild $Path/nologo/v:q/consoleloggerparameters:ErrorsOnly |&$输出

<小时>

更新

上面的代码丢失了 MSBuild 颜色.为了保留颜色又避免可以使用这种方法的重复代码:

# 将命令定义为一个脚本块$command = {msbuild $Path/nologo/v:q/consoleloggerparameters:ErrorsOnly}# 根据条件调用带有输出的命令if ($SolutionsWithErrors -eq 0) {&$command} else {&$命令|出空}

<小时><块引用>

有没有一种方法可以运行命令,但仅在满足特定条件时才通过管道传输其输出(否则根本不会通过管道传输或重定向其输出,因此它可以执行诸如彩色编码输出之类的奇特事情)?

没有内置的这种方式,更有可能.但是它可以用一个函数来实现,并且该函数以这样的方式被重用:

function Invoke-WithOutput($OutputCondition, $Command) {if ($OutputCondition) { &$Command } else { $null = &$命令}}Invoke-WithOutput ($SolutionsWithErrors -eq 0) {msbuild $Path/nologo/v:q/consoleloggerparameters:ErrorsOnly}

I'm writing a PowerShell script to msbuild a bunch of solutions. I want to count how many solutions build successfully and how many fail. I also want to see the compiler errors, but only from the first one that fails (I'm assuming the others will usually have similar errors and I don't want to clutter my output).

My question is about how to run an external command (msbuild in this case), but conditionally pipe its output. If I'm running it and haven't gotten any failures yet, I don't want to pipe its output; I want it to output directly to the console, with no redirection, so it will color-code its output. (Like many programs, msbuild turns off color-coding if it sees that its stdout is redirected.) But if I have gotten failures before, I want to pipe to Out-Null.

Obviously I could do this:

if ($SolutionsWithErrors -eq 0) {
    msbuild $Path /nologo /v:q /consoleloggerparameters:ErrorsOnly
} else {
    msbuild $Path /nologo /v:q /consoleloggerparameters:ErrorsOnly | Out-Null
}

But it seems like there's got to be a way to do it without the duplication. (Okay, it doesn't have to be duplication -- I could leave off /consoleloggerparameters if I'm piping to null anyway -- but you get the idea.)

There may be other ways to solve this, but for today, I specifically want to know: is there a way to run a command, but only pipe its output if a certain condition is met (and otherwise not pipe it or redirect its output at all, so it can do fancy stuff like color-coded output)?

解决方案

You can define the output command as a variable and use either Out-Default or Out-Null:

# set the output command depending on the condition
$output = if ($SolutionsWithErrors -eq 0) {'Out-Default'} else {'Out-Null'}

# invoke the command with the variable output
msbuild $Path /nologo /v:q /consoleloggerparameters:ErrorsOnly | & $output


UPDATE

The above code loses MSBuild colors. In order to preserve colors and yet avoid duplication of code this approach can be used:

# define the command once as a script block
$command = {msbuild $Path /nologo /v:q /consoleloggerparameters:ErrorsOnly}

# invoke the command with output depending on the condition
if ($SolutionsWithErrors -eq 0) {& $command} else {& $command | Out-Null}


is there a way to run a command, but only pipe its output if a certain condition is met (and otherwise not pipe it or redirect its output at all, so it can do fancy stuff like color-coded output)?

There is no such a way built-in, more likely. But it can be implemented with a function and the function is reused as such a way:

function Invoke-WithOutput($OutputCondition, $Command) {
    if ($OutputCondition) { & $Command } else { $null = & $Command }
}

Invoke-WithOutput ($SolutionsWithErrors -eq 0) {
    msbuild $Path /nologo /v:q /consoleloggerparameters:ErrorsOnly
}

这篇关于有条件地管道到 Out-Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆