powershell - Define custom property sets (with Add-Member?) for use in Select-Object -
what seek quite simple: create custom object properties, , define "groups" of properties (columns) utilize in select-object. allow me clarify:
$props = @{"mary"=1;"jane"=2;"frank"=3;"john"=5;"brenda"=6} $obj = new-object psobject $props i have custom object bogus data. want able is
$obj | select male $obj | select female and had thought trick, this:
$obj | add-member propertyset "male" @("frank","john") $obj | add-member propertyset "female" @("mary","jane","brenda") it doesn't work - error:
add-member : cannot convert "system.object[]" value of type "system.object[]" type "system.collections.objectmodel.collection`1[system.string]". i guess should provide object type array add-member, i'm unsure how should that.
does have experience this?
important note: i'm on powershell 2, , read on various sites has bug doesn't allow setting default properties. that's not want - want create custom property set , not default 1 - bug prevents me getting want.
you close. problem you're not creating object correctly. need specify -property parameter before specify hashtable of properties. without it, create hashtable. works:
$props = @{"mary"=1;"jane"=2;"frank"=3;"john"=5;"brenda"=6} $obj = new-object -typename psobject -property $props $obj | add-member propertyset "male" @("frank","john") $obj | add-member propertyset "female" @("mary","jane","brenda") $obj | select male frank john ----- ---- 3 5 why did happend? if read syntax new-object using get-help new-object or get-command new-object -syntax, see normal .net types, syntax is:
new-object [-typename] <string> [[-argumentlist] <object[]>] [-property <idictionary>] notice -argumentlist 2nd paramter, not -property expected. code did:
$obj = new-object -typename psobject -argumentlist $props instead of:
$obj = new-object psobject -property $props edit solution above worked in ps3.0 . it's still valid though, -property parameter required in ps2.0 also. in ps2.0 need cast propertyset-array string[] (string-array) , not object-array (object[]) default array. finish solution ps2.0 is:
$props = @{"mary"=1;"jane"=2;"frank"=3;"john"=5;"brenda"=6} $obj = new-object -typename psobject -property $props $obj | add-member propertyset "male" ([string[]]@("frank","john")) $obj | add-member propertyset "female" ([string[]]@("mary","jane","brenda")) $obj | select male frank john ----- ---- 3 5 powershell custom-object pscustomobject
No comments:
Post a Comment