代码之家  ›  专栏  ›  技术社区  ›  Paul Moore

是否可以自定义powershell中的错误显示?

  •  13
  • Paul Moore  · 技术社区  · 16 年前

    4 回复  |  直到 16 年前
        1
  •  13
  •   Jason Plank Maksim Kondratyuk    13 年前

    是的,是的。

    您可以使用内置的 $host 对象,如果您只想更改文本颜色。但是,您不能更改错误消息本身-这是硬编码的。

    您可以做的是(a)抑制错误消息,而不是(b)捕获错误并显示您自己的错误消息。

    $ErrorActionPreference = "SilentlyContinue" -这不会停止错误,但会抑制消息。

    完成(b)需要更多的工作。默认情况下,大多数PowerShell命令不会生成可捕获的异常。因此,您必须学习运行命令并添加-EA“Stop”参数,以便在出现问题时生成可捕获的异常。完成此操作后,可以通过键入以下内容在shell中创建陷阱:

    trap {
     # handle the error here
    }
    

    可能比你想做的要多,但基本上你就是这样做的。

        2
  •  9
  •   JasonMArcher TWE    13 年前

    这里有一些东西可以让你定制你的控制台输出。您可以在配置文件中随意设置这些设置,也可以使用函数/脚本来更改这些设置,以实现不同的目的。也许你有时候想要一个“不要打扰我”的模式,或者在其他时候想要一个“告诉我一切都出了问题”。您可以制作一个函数/脚本在这些函数/脚本之间进行更改。

    ## 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
    

    您还可以在cmdlet上使用-ErrorAction和-ErrorVariable参数,以仅影响该cmdlet调用。第二个将向指定变量发送错误,而不是默认的$Error。

        3
  •  3
  •   Charlie Joynt S. Barij    8 年前

    这可能不是你想要的,但有一个 可以设置的首选项变量:

    $ErrorView = "CategoryView"
    

    这将提供较短的单行错误消息,例如:

    [PS]> get-item D:\blah
    ObjectNotFound: (D:\blah:String) [Get-Item], ItemNotFoundException
    
        4
  •  1
  •   Neil    14 年前

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

    (克里斯·西尔斯的回答)