14

为什么 Powershell 的Write-Errorcmdlet 不适合我?我的输出看起来不像文档中的示例:

PS C:\> Write-Error "This is an error"
Write-Error "This is an error" : This is an error
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

我一直期望输出类似于Write-Warning

PS H:\> Write-Warning "This is a warning"
WARNING: This is a warning

Write-Errorabout_preference_variables文档中,我认为我不应该看到任何异常?

PS H:\> Get-Help About_Preference_Variables

$ErrorActionPreference
----------------------

...

        PS> $erroractionpreference                      
        Continue        # Display the value of the preference.                

        PS> write-error "Hello, World"                  
                                # Generate a non-terminating error.

        write-error "Hello, World" : Hello, World       
                                # The error message is displayed and
                                  execution continues.

        PS> write-error "Hello, World" -ErrorAction:SilentlyContinue
                                # Use the ErrorAction parameter with a 
                                  value of "SilentlyContinue".
        PS>                                             
                                # The error message is not displayed and
                                  execution continues.
4

4 回答 4

17

要获得类似于 write-warning 的输出,您可以这样做:

$Host.UI.WriteErrorLine("This is an error")

(克里斯西尔斯的这个答案的道具)

于 2011-04-13T19:53:31.730 回答
6

你为什么不认为它有效?请记住,PowerShell 会区分上述非终止错误和执行时遇到的终止错误throw 'Access denied.'。非终止错误被写入 stderr 并记录在 $error 集合中,但它们不会停止脚本的处理。当您处理(例如删除或复制)一堆文件时,此功能非常方便。您想知道哪些文件无法处理,但您不希望整个操作在第一个出错的文件上停止。

PowerShell 还为您提供了将非终止错误“转换”为终止错误的选项,例如

Remove-Item c:\file-doesnt-exist -ErrorAction Stop; "Did I get here"

请注意,在这种情况下,执行会停止并且不会在最后打印出字符串。不带 试试看-ErrorAction Stop,你会看到错误,但你也会看到字符串“我到这里了吗”。

如果要控制类别信息,可以使用 -Category 参数,如下所示:

PS> write-error "foo" -Category 'InvalidResult'
write-error "foo" -Category 'InvalidResult' : foo
    + CategoryInfo          : InvalidResult: (:) [Write-Error], WriteErrorExce..
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

但是 WriteErrorException 是这个 cmdlet 引发错误的机制(我认为)。有一个Exception参数,但我没有太多运气使用它。

于 2011-03-11T00:10:25.367 回答
3

我相信您会看到该 cmdlet 的预期输出。您正在输入“拒绝访问”。参数并将其输出到主机,并且最有可能按照设计输出到错误流。您可以确认它正在输出到 $Error 变量,并且应该填充您刚刚插入的错误。

IE

PS C:\> $error.Clear()

PS C:\> Write-Error "access denied"

Write-Error "access denied" : access denied

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

PS C:\> $error

Write-Error "access denied" : access denied

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

不过,我可以看到哪里会令人困惑,也许 MSFT 应该将示例错误从“拒绝访问”之类的内容更改为“Foobar”,以便清楚起见。

编辑以解决进一步的问题:Write-Error 的默认 errorAction 是“继续”,因此要使其表现得像 Write-Warning,您必须添加 -ErrorAction SilentlyContinue。考虑以下示例:

PS E:\> $error.clear()
PS E:\> Write-Error 'test'
Write-Error 'test' : test
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

PS E:\> Write-Error 'test2' -ErrorAction silentlycontinue  

PS E:\> $error[1]
Write-Error 'test' : test
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

PS E:\> $error[0]
Write-Error 'test2' -ErrorAction silentlycontinue : test2
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
于 2011-03-10T21:42:43.800 回答
1

这里的问题是我们假设了一种用于打印详细信息、信息、警告和错误消息的模式,并期望Write-cmdlet 形成一个遵循此模式的集合。

但是Write-Error是不同的,并且令人怀疑这种模式是作者的意图还是仅仅是程序员的想法。这是 API 设计的煤气灯。

写详细

Write-Verbose cmdlet 将文本写入 PowerShell 中的详细消息流

写警告

Write-Warning cmdlet 将警告消息写入 PowerShell 主机。

写错误

Write-Error cmdlet 声明一个非终止错误。

哦。它做了一些不同的事情。它“声明”了一个错误。这就像引发一个事件。它不是将文本写入标准错误....

于 2021-10-29T17:52:14.037 回答