【发布时间】:2021-10-30 01:52:14
【问题描述】:
为什么 Powershell 的 Write-Error cmdlet 不适合我?我的输出看起来不像文档中的示例:
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-Error 和about_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.
【问题讨论】:
-
您希望在输出中看到什么?你发布的那个在我看来完全正常。
-
@zespri 我一直在期待类似于 Write-Warning 的内容...我已经编辑了问题以包含一个示例。
-
您看到的输出是正确的。输出随着 v2.0 IIRC 的变化而变得更加冗长。文档不正确 - 这不是第一次发生。 :-)
-
你来到这里是因为
Write-Error违反了最小意外原则。