显然有一个通过设计怪癖 https://connect.microsoft.com/PowerShell/feedback/details/782806/calculated-properties-do-not-throw-exceptions在 Powershell 中防止在内部抛出异常计算属性 http://technet.microsoft.com/en-us/library/ff730948.aspx表情冒泡起来。所发生的只是计算属性的值最终为空。
function Get-KBValue() {
# Some Logic here that can throw an exception
}
....
Get-ChildItem C:\Test |
Select-Object Name, CreationTime, @{Name="Kbytes"; Expression={ Get-KBValue }}
If the Get-KBValue
函数抛出异常,然后返回值Kbytes
属性设置为$null
脚本继续。
可能的解决方法:
- Use
try/catch{break}
在表达式中(由@C.B.建议)
- 之后验证。尽管这可能会因为以下事实而变得复杂
$null
在某些情况下可能有效。
- 使用自定义对象而不是计算属性。但这并不那么好。
有什么想法吗?
在表达式中使用 try/catch 可以帮助你吗?
10..0 | SELECT @{n="Value";e={ try { 10/$_ } catch { "error: $_" }}}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)