Pau*_*ore 14 powershell customization
我发现标准的Powershell错误显示(红色文字,多行显示)有点分散注意力.可以自定义吗?
Don*_*nes 13
是的,是的.
$host
如果您只想更改文本颜色,则可以使用内置对象.但是,您无法更改错误消息本身 - 这是硬编码的.
您可以做的是(a)抑制错误消息,而不是(b)捕获错误并显示您自己的错误.
通过设置完成(a)$ErrorActionPreference = "SilentlyContinue"
- 这不会停止错误,但它会抑制消息.
完成(b)需要更多的工作.默认情况下,大多数PowerShell命令不会产生可捕获的异常.因此,您必须学习运行命令并添加-EA"Stop"参数,以便在出现问题时生成可捕获的异常.完成后,您可以通过键入以下命令在shell中创建陷阱:
trap {
# handle the error here
}
Run Code Online (Sandbox Code Playgroud)
你可以将它放在你的个人资料脚本中,而不是每次都输入它.在陷阱内,您可以使用Write-Error cmdlet输出您喜欢的任何错误文本.
可能比你想做的更多的工作,但这基本上是你做你所要求的.
这里有一堆东西可以让你自定义你的控制台输出.您可以在配置文件中根据需要设置这些设置,也可以将功能/脚本更改为不同用途.也许你有时想要"别烦我"模式,或者别人想要"告诉我一切都出错".您可以创建一个函数/脚本来在这些之间进行更改.
## Change colors of regular text
$Host.UI.RawUI.BackGroundColor = "DarkMagenta"
$Host.UI.RawUI.ForeGroundColor = "DarkYellow"
## Change colors of special messages (defaults shown)
$Host.PrivateData.DebugBackgroundColor = "Black"
$Host.PrivateData.DebugForegroundColor = "Yellow"
$Host.PrivateData.ErrorBackgroundColor = "Black"
$Host.PrivateData.ErrorForegroundColor = "Red"
$Host.PrivateData.ProgressBackgroundColor = "DarkCyan"
$Host.PrivateData.ProgressForegroundColor = "Yellow"
$Host.PrivateData.VerboseBackgroundColor = "Black"
$Host.PrivateData.VerboseForegroundColor = "Yellow"
$Host.PrivateData.WarningBackgroundColor = "Black"
$Host.PrivateData.WarningForegroundColor = "Yellow"
## Set the format for displaying Exceptions (default shown)
## Set this to "CategoryView" to get less verbose, more structured output
## http://blogs.msdn.com/powershell/archive/2006/06/21/641010.aspx
$ErrorView = "NormalView"
## NOTE: This section is only for PowerShell 1.0, it is not used in PowerShell 2.0 and later
## More control over display of Exceptions (defaults shown), if you want more output
$ReportErrorShowExceptionClass = 0
$ReportErrorShowInnerException = 0
$ReportErrorShowSource = 1
$ReportErrorShowStackTrace = 0
## Set display of special messages (defaults shown)
## http://blogs.msdn.com/powershell/archive/2006/07/04/Use-of-Preference-Variables-to-control-behavior-of-streams.aspx
## http://blogs.msdn.com/powershell/archive/2006/12/15/confirmpreference.aspx
$ConfirmPreference = "High"
$DebugPreference = "SilentlyContinue"
$ErrorActionPreference = "Continue"
$ProgressPreference = "Continue"
$VerbosePreference = "SilentlyContinue"
$WarningPreference = "Continue"
$WhatIfPreference = 0
Run Code Online (Sandbox Code Playgroud)
您还可以在cmdlet上使用-ErrorAction和-ErrorVariable参数来仅影响该cmdlet调用.第二个将错误发送到指定的变量而不是默认的$ Error.
归档时间: |
|
查看次数: |
12335 次 |
最近记录: |