禁止来自非PowerShell命令的输出

禁止来自非PowerShell命令的输出

本文介绍了禁止来自非PowerShell命令的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行命令

hg st

,然后检查它是$LASTEXITCODE,以检查当前目录中是否有汞.我不在乎它的输出,也不想将其显示给我的用户.

and then checking it's $LASTEXITCODE to check for availability of mercurial in the current directory. I do not care about its output and do not want to show it to my users.

如何抑制所有输出,成功或错误?

How do I suppress ALL output, success or error?

由于Mercurial不是PowerShell命令集,因此hg st | Out-Null不起作用.

Since mercurial isn't a PowerShell commandlet hg st | Out-Null does not work.

推荐答案

Out-Null在非PowerShell命令中也可以正常工作.但是,它不抑制STDERR上的输出,仅抑制STDOUT上的输出.如果还要抑制在STDERR上的输出,则必须在将输出传递到Out-Null之前将文件描述符重定向到STDOUT:

Out-Null works just fine with non-PowerShell commands. However, it doesn't suppress output on STDERR, only on STDOUT. If you want to suppress output on STDERR as well you have to redirect that file descriptor to STDOUT before piping the output into Out-Null:

hg st 2>&1 | Out-Null

2>重定向STDERR(文件描述符#2)的所有输出. &1将重定向的输出与STDOUT的输出(文件描述符#1)合并.然后将合并的输出打印到STDOUT,管道可以在此处将其输出到管道中下一个命令的STDIN中(在本例中为Out-Null).有关更多信息,请参见 Get-Help about_Redirection .

2> redirects all output from STDERR (file descriptor #2). &1 merges the redirected output with the output from STDOUT (file descriptor #1). The combined output is then printed to STDOUT from where the pipe can feed it into STDIN of the next command in the pipline (in this case Out-Null). See Get-Help about_Redirection for further information.

这篇关于禁止来自非PowerShell命令的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:36