支持所有版本。
如果你需要在脚本中输出错误信息,你可以使用 “Write-Warning” 或 “Write-Error”.两者的显示颜色将与内置的错误与警告相同,当然它们也可以自定义输出模版:
PS> Write-Warning -Message 'This is a warning' WARNING: This is a warning PS> Write-Error -Message 'Something went wrong' Write-Error -Message 'Something went wrong' : Something went wrong + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
Write-Error添加了更多的详细的错误信息,如果你不需要这些附加的信息,你可以试试下面代码:
PS> $host.UI.WriteErrorLine('Something went wrong...') Something went wrong...
对于它们的输出颜色,可以这样定义:
PS> $host.UI.WriteErrorLine('Something went wrong...') Something went wrong... PS> $host.PrivateData.ErrorBackgroundColor = 'White' PS> $host.UI.WriteErrorLine('Something went wrong...') Something went wrong... PS> $host.PrivateData (...) ErrorForegroundColor : #FFFF0000 ErrorBackgroundColor : #FFFFFFFF WarningForegroundColor : #FFFF8C00 WarningBackgroundColor : #00FFFFFF VerboseForegroundColor : #FF00FFFF VerboseBackgroundColor : #00FFFFFF DebugForegroundColor : #FF00FFFF DebugBackgroundColor : #00FFFFFF (...)
原文地址:Using System Error Colors for Output
本文链接: https://www.pstips.net/using-system-error-colors-for-output.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!