PS C:\> $array
ReadWrite(AllHostsSpecified) :
ReadOnly(AllHostsSpecified) :
ReadWrite(NegateSpecified) :
ReadWrite(Negate) :
SecFlavor : sys
ActualPathname :
ReadOnly(AllHosts) :
ReadOnly(Name) :
ReadWrite(Name) :
Anon :
Root : {vin1,vin2,vin3,vin4...}
ReadOnly(NegateSpecified) :
ReadWrite(AllHosts) :
NosuidSpecified : False
Nosuid :
ReadOnly(Negate) :
Pathname : /vol/vin_binaries
嗨,大家好,我有属性root,这是一个动态属性,可以从(vin1,vin2,vin3,vin4 .......)变化,甚至可以变得无穷无尽。
现在当我执行export-csv时,我需要获取输出对象,如下所示
ReadWrite(AllHostsSpecified) :
ReadOnly(AllHostsSpecified) :
ReadWrite(NegateSpecified) :
ReadWrite(Negate) :
SecFlavor : sys
ActualPathname :
ReadOnly(AllHosts) :
ReadOnly(Name) :
ReadWrite(Name) :
Anon :
Root (Property 1) : vin1
Root (Property 1) : vin2
Root (Property 1) : vin3
Root (Property 1) : vin4
.
.
.
.
Root (Property n) : vinn
ReadOnly(NegateSpecified) :
ReadWrite(AllHosts) :
NosuidSpecified : False
Nosuid :
ReadOnly(Negate) :
Pathname : /vol/vin_binaries
有没有办法可以实现?喜欢在for循环中迭代Root ???
的所有属性答案 0 :(得分:0)
尝试这样的事情:
filter ExpandProperties {
$obj = $_
$obj | gm -MemberType *property | % {
#Find objects with array value
if( @($obj.($_.Name)).Count -gt 1 ) {
$count = 1
$prop = $_.Name
$obj.($prop) | % {
#Foreach value in the property
$obj | Add-Member NoteProperty -Name "$prop (Property $($count))" -Value $_
$count++
}
}
}
#Output object
$obj
}
$o1 = New-Object psobject -Property @{
Name = @("Name1", "Name2")
Root = @("vin1","vin2")
}
$o2 = New-Object psobject -Property @{
Name = "Name2"
Root = @("vin1","vin2","vin3")
}
$o = $o1, $o2
$o | ExpandProperties
Name : {Name1, Name2}
Root : {vin1, vin2}
Name (Property 1) : Name1
Name (Property 2) : Name2
Root (Property 1) : vin1
Root (Property 2) : vin2
Name : Name2
Root : {vin1, vin2, vin3}
Root (Property 1) : vin1
Root (Property 2) : vin2
Root (Property 3) : vin3
这也显示了原始数组属性(例如“Root”)。如果要排除它,则需要在函数内使用select -exclude ...
。但是,这会创建一个新的“pscustomobject”(你丢失了原始的objecttype),所以我没有在上面的解决方案中包含它。