Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何生成与echo相同的写入错误输出?_Powershell - Fatal编程技术网

Powershell 如何生成与echo相同的写入错误输出?

Powershell 如何生成与echo相同的写入错误输出?,powershell,Powershell,我已经了解了echo(这是一种写入输出)和写入错误的区别,但我仍然存在异常和堆栈跟踪问题 以下示例演示了我的问题: $ErrorActionPreference = "stop" trap { echo "Trap encountered. Exiting with 1. See errors below." echo $_ # This is a test Write-Error -ErrorRecord $_ exit 1 } BROKEN_COMMAN

我已经了解了
echo
(这是一种
写入输出
)和
写入错误
的区别,但我仍然存在异常和堆栈跟踪问题

以下示例演示了我的问题:

$ErrorActionPreference = "stop"
trap  {
    echo "Trap encountered. Exiting with 1. See errors below."
    echo $_ # This is a test
    Write-Error -ErrorRecord $_

    exit 1
}

BROKEN_COMMAND
我不会发布输出,因为它是本地化的,但要点是:

  • echo
    outputs“breaked_COMMAND”未被发现是CmdLet etc,并输出发现问题的确切位置的调用堆栈,位于第10行左右
  • Write Error
    输出未发现行“breake_COMMAND”也是CmdLet,但是调用堆栈实际上显示了Write Error语句所在的行,而不是正确的调用堆栈(第5行)
如何正确写入错误通道?我试图省略
-ErrorRecord
,但没有效果

我需要错误通道,所以在echo中转储所有内容,然后在错误通道中写入“有错误”不是一个好选项


更新:我用
throw
(实际上是
throw$
)测试了@MathiasR.Jessen提示。 但是,这会导致程序立即退出,
$LASTEXITCODE
中没有有用的退出代码,我需要它来正确地发出结束信号。My
退出1
未执行,且抛出在没有正确退出代码的情况下终止。

请参阅

write error cmdlet将显示您的消息,后跟write error cmdlet本身的行号

在相关问题中,有人说这种行为在某个时候发生了变化

无论哪种情况。。试试这个 在
Get Help About_Preference_Variables
中提到了
$ErrorView
变量

法线视图(默认)

但是如果你
$ErrorView=“CategoryView”
你得到

也许这更有用

你可能想改变你的想法 回音$_ 到 echo$\.tostring()文件

也看到


很遗憾,您不能编写自己的错误表单,如log4

有趣的阅读:如果您想保留原始错误,请使用
throw
(无参数)而不是
write error$重新抛出错误callstack@MathiasR.Jessen扔不起作用。写
throw$\
不管如何修复:)我没有考虑throw,因为我在陷阱中,这里假设了递归。@MathiasR.Jessen这实际上对于
throw$\
仍然是无用的,因为它退出了我的程序,没有指定正确的退出代码,当异常退出时我需要退出代码,这是古怪的powershell行为。
Trap encountered. Exiting with 1. See errors below.
BROKEN_COMMAND : The term 'BROKEN_COMMAND' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
At C:\errors.ps1:11 char:1
+ BROKEN_COMMAND
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (BROKEN_COMMAND:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

C:\errors.ps1 : The term 'BROKEN_COMMAND' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again.
At C:\errors.ps1:6 char:6
+      Write-Error -ErrorRecord $_
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (BROKEN_COMMAND:String) [Write-Error], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,errors.ps1
Trap encountered. Exiting with 1. See errors below.
ObjectNotFound: (BROKEN_COMMAND:String) [], CommandNotFoundException
ObjectNotFound: (BROKEN_COMMAND:String) [Write-Error], CommandNotFoundException