使用 Write-Error 在 Powershell 中写入错误流 [英] Writing to error stream in Powershell using Write-Error
本文介绍了使用 Write-Error 在 Powershell 中写入错误流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么 Powershell 的 Write-Error
cmdlet 对我有用吗?我的输出看起来不像文档中的示例:
Why isn't Powershell's Write-Error
cmdlet working for me? My output doesn't look like the examples in the documentation:
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
:
I had been expecting output similar to Write-Warning
:
PS H:\> Write-Warning "This is a warning"
WARNING: This is a warning
来自 Write-Error
和 about_preference_variables
文档 我认为我不应该看到任何异常?
From the Write-Error
and about_preference_variables
documentation I thought I should not see any exceptions?
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.
推荐答案
要获得类似于 write-warning 的输出,您可以这样做:
To get output similar to write-warning, you can do this:
$Host.UI.WriteErrorLine("This is an error")
(支持克里斯·西尔斯回答这个问题)
(props to Chris Sears for this answer)
这篇关于使用 Write-Error 在 Powershell 中写入错误流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文