How can I display a 'naked' error message in PowerShell without an accompanying stacktrace?

后端 未结 5 1912
星月不相逢
星月不相逢 2021-02-05 04:25

How can I write to standard error from PowerShell, or trap errors such that:

  • An error message is displayed as an error (truly writing to standard error so that Tea
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 04:57

    I needed to solve this problem myself recently so I put together a Write-ErrorMessage function as detailed here: https://intellitect.com/powershell-write-error-without-writing-stack-trace/

    Specifically, I leveraged the combination

    Write-Error -Message $err -ErrorAction SilentlyContinue
    $Host.UI.WriteErrorLine($errorMessage)
    

提交回复
热议问题