如何在 PowerShell 中写入标准错误? [英] How do I write to standard error in PowerShell?

查看:32
本文介绍了如何在 PowerShell 中写入标准错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何回显到标准错误流和重定向可执行文件的错误流.

I'm having trouble figuring out how to both echo to the standard error stream and redirect the error stream of an executable.

我来自 Bourne shellKorn shell 背景,我会使用;

I have come from a Bourne shell and Korn shell background, of which I would use;

# Write to stderr
echo "Error Message!" >&2

# Redirect stderr to file
/do/error 2>/tmp/err.msg

推荐答案

使用 Write-Error 写入 stderr.将 stderr 重定向到文件使用:

Use Write-Error to write to stderr. To redirect stderr to file use:

 Write-Error "oops" 2> /temp/err.msg

 exe_that_writes_to_stderr.exe bogus_arg 2> /temp/err.msg

请注意,PowerShell 将错误写入错误记录.如果你想避免错误记录的冗长输出,你可以像这样自己写出错误信息:

Note that PowerShell writes errors as error records. If you want to avoid the verbose output of the error records, you could write out the error info yourself like so:

PS> Write-Error "oops" -ev ev 2>$null
PS> $ev[0].exception
oops

-EV-ErrorVariable 的简称(别名).任何错误都将存储在由该参数的参数命名的变量中.除非我们将错误重定向到 $null,否则 PowerShell 仍会向控制台报告错误.

-EV is short (an alias) for -ErrorVariable. Any errors will be stored in the variable named by the argument to this parameter. PowerShell will still report the error to the console unless we redirect the error to $null.

这篇关于如何在 PowerShell 中写入标准错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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