Variablize Method Call in Powershell -
i'm looking way dynamically call method or property of function. example wanted call every property of $error[0]. list of property names with:
$a = ($error[0] | get-member -type property | select name)
i this:
foreach($b in $a){ $error[0].&($b.name) }
but call operator (&) doesn't resolve $b.name expect (it should resolve 'catagoryinfo'). there anyway this?
you use different way retrieve properties:
$properties=$error[0].psobject.properties
this return collection of pspropertyinfo
objects. pspropertyinfo
objects have value
property use or set property value:
$properties|foreach-object {'name={0}; value={1}'-f$_.name,$_.value}
also note of possible problems in way of retrieving properties' values. powershell allows access idictionary
elements property syntax. problem here powershell prefer access collection element rather actual property:
$hashtable=@{} $hashtable.count # 0 $hashtable.add('somename',10) $hashtable.somename # 10 $hashtable.count # 1 $hashtable.add('count',20) $hashtable.count # 20 $hashtable|select-object -expandproperty count # 2
Comments
Post a Comment