如何从PowerShell写入标准错误,或捕获错误,以便:
这些年来,我一直通过throw的错误或通过Write-Error进行写作而幸存下来,但我又累又老,在我的脚本中,我只想看到一条简明的错误消息。我一直在尝试trap、throw、Write-Error和-ErrorAction的每一个组合,但都没有结果:
try {
throw "error" # Sample code for a stack overflow. In the theater
# of your mind, imagine there is code here that does something real and useful
} catch {
Write-Error "An error occurred attempting to 'do something.' Have you tried rebooting?"
}下面是我想要看到的用户体验:
C:\> & .\Do-Something.ps1
An error occurred attempting to 'do something.' Have you tried rebooting?
C:\> ▏相反,我得到了:
C:\> & .\Do-Something.ps1
An error occurred attempting to 'do something.' Have you tried rebooting?
At line:1 char:1
+ Do-RealWork
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Do-RealWork
C:\> ▏发布于 2016-12-30 14:29:37
在前一个答案中的想法上构建时,可以使用自定义函数临时覆盖内置的写错误cmdlet .
# Override the built-in cmdlet with a custom version
function Write-Error($message) {
[Console]::ForegroundColor = 'red'
[Console]::Error.WriteLine($message)
[Console]::ResetColor()
}
# Pretty-print "Something is wrong" on stderr (in red).
Write-Error "Something is wrong"
# Setting things back to normal
Remove-Item function:Write-Error
# Print the standard bloated Powershell errors
Write-Error "Back to normal errors"这样,您就可以利用Powershell函数优先于cmdlet这一事实。
https://technet.microsoft.com/en-us/library/hh848304.aspx
这是我想出的最优雅的方法,既可以显示漂亮而简洁的错误消息,也可以让TeamCity很容易地检测问题。
发布于 2018-04-17 05:04:15
最近我需要自己解决这个问题,所以我编写了一个写错误消息函数,具体如下所示:https://intellitect.com/powershell-write-error-without-writing-stack-trace/
具体来说,我利用了
Write-Error -Message $err -ErrorAction SilentlyContinue
$Host.UI.WriteErrorLine($errorMessage)发布于 2016-06-27 23:59:43
在我看来,在PowerShell中捕获错误的最佳方法是使用以下方法:
$Error[0].Exception.GetType().FullName下面是一个如何正确使用此方法的示例。基本上,用脚本失败的不同场景测试您在PowerShell中要做的事情。
下面是一条典型的PowerShell错误消息:
PS C:\> Stop-Process -Name 'FakeProcess'
Stop-Process : Cannot find a process with the name "FakeProcess". Verify the process name and call the cmdlet again.
At line:1 char:1
+ Stop-Process -Name 'FakeProcess'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (FakeProcess:String) [Stop-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.StopProcessCommand接下来,您将得到错误消息的例外:
PS C:\> $Error[0].Exception.GetType().FullName
Microsoft.PowerShell.Commands.ProcessCommandException您将设置代码以捕获错误消息,如下所示:
Try
{
#-ErrorAction Stop is needed to go to catch statement on error
Get-Process -Name 'FakeProcess' -ErrorAction Stop
}
Catch [Microsoft.PowerShell.Commands.ProcessCommandException]
{
Write-Host "ERROR: Process Does Not Exist. Please Check Process Name"
}输出将类似于下面的内容,而不是上面示例中的Powershell标准错误:
ERROR: Process Does Not Exist. Please Check Process Name最后,还可以使用多个catch块来处理代码中的多个错误。您还可以包括一个“毯子”catch块,以捕获所有尚未处理的错误。示例:
Try
{
Get-Process -Name 'FakeProcess' -ErrorAction Stop
}
Catch [Microsoft.PowerShell.Commands.ProcessCommandException]
{
Write-Host "ERROR: Process Does Not Exist. Please Check Process Name"
}
Catch [System.Exception]
{
Write-Host "ERROR: Some Error Message Here!"
}
Catch
{
Write-Host "ERROR: I am a blanket catch to handle all unspecified errors you aren't handling yet!"
}https://stackoverflow.com/questions/38064704
复制相似问题