gpt4 book ai didi

powershell - 如何在 PowerShell 中写入标准错误?

转载 作者:行者123 更新时间:2023-12-03 07:07:14 24 4
gpt4 key购买 nike

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

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

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

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

最佳答案

使用Write-Error写入stderr。要将 stderr 重定向到文件,请使用:

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

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

请注意,PowerShell 将错误写入错误记录。如果你想避免错误记录的详细输出,你可以自己写出错误信息,如下所示:

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

-EV-ErrorVariable 的缩写(别名)。任何错误都将存储在此参数的参数命名的变量中。 PowerShell 仍会向控制台报告错误,除非我们将错误重定向到 $null

关于powershell - 如何在 PowerShell 中写入标准错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4998173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com