Calculated properties do not throw exceptions in Powershell. What are the workaround possibilities? -


apparently there by design quirk in powershell prevents exceptions thrown inside calculated property expression bubbling up. happens value of calculated property ends being null.

function get-kbvalue() {     # logic here can throw exception }  ....  get-childitem c:\test |      select-object name, creationtime,  @{name="kbytes"; expression={ get-kbvalue }} 

if get-kbvalue function throws exception value of kbytes property set $null , script continues.

possible workarounds:

  • use try/catch{break} within expression (suggested @c.b.)
  • validate afterwards. although might complicated fact $null valid in cases.
  • use custom object instead of calculated property. not nice.

any thoughts?

use try/cacth in expression can you?

10..0 | select @{n="value";e={ try { 10/$_ } catch { "error: $_" }}} 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -