我在Powershell w.r.t中遇到了(对我而言)非常令人惊讶的错误抑制行为。到Select-Xml Commandlet。为了显示(IMHO)的预期行为,我首先显示一个Get-Content
的最小示例:
try {
Get-Content -Path "NonExistingFile.txt"
}
finally {
exit 42
}
如预期的那样,这将引发错误(包括消息):
PS > .\gc-nonexistingfile.ps1
Get-Content : Der Pfad "NonExistingFile.txt" kann nicht gefunden werden, da er nicht
vorhanden ist.
In gc-nonexistingfile.ps1:2 Zeichen:5
+ Get-Content -Path "NonExistingFile.txt"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (...xistingFile.txt:String) [Get-Content],
ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
PS > echo $LASTEXITCODE
42
如果我对Select-Xml
做同样的事情,错误消息将被吞没,但是仅当我使用exit
关键字时。如果我删除了exit
行,则报告的错误如上所述:
try {
Select-Xml -Path "NonExistingFile.xml" -XPath "*"
}
finally {
exit 43 # comment this out to get error message
}
退出行为:
PS > .\sx-nonexistingfile.ps1
PS > echo $LASTEXITCODE
43
没有退出(为清楚起见直接调用命令行开关):
PS > Select-Xml -Path "NonExistingFile.xml" -XPath "*"
Select-Xml : Der Pfad "NonExistingFile.xml" kann nicht gefunden werden, da er nicht
vorhanden ist.
In Zeile:1 Zeichen:1
+ Select-Xml -Path "NonExistingFile.xml" -XPath "*"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (...xistingFile.xml:String) [Select-Xml],
ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SelectXmlCommand
我的问题:为什么有区别?并且:尽管我使用Select-Xml
结束脚本,我怎么能exit
明显地抛出错误?
答案 0 :(得分:0)
在@Mathias R. Jessen's suggestion in the comment之后,我确定的最佳解决方案是脚本中的catch
块:
有了这个
function Report-Error ($Error)
{
$FormatString = "{0} : {1}`n{2}`n" +
" + CategoryInfo : {3}`n" +
" + FullyQualifiedErrorId : {4}`n"
$ErrorFields = $Error.InvocationInfo.MyCommand.Name,
$Error.ErrorDetails.Message,
$Error.InvocationInfo.PositionMessage,
$Error.CategoryInfo.ToString(),
$Error.FullyQualifiedErrorId
Write-Host -Foreground Red -Background Black ($FormatString -f $ErrorFields)
}
脚本变为:
try {
Select-Xml -Path "NonExistingFile.xml" -XPath "*"
}
catch {
Report-Error $_
}
finally {
exit 43
}