简体   繁体   English

迭代PowerShell对象的子属性

[英]Iterate Sub properties of PowerShell Objects

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

Hi Guys, i have property root which is a dynamic property and which can vary from (vin1,vin2,vin3,vin4.......) which can also even get endless. 嗨,大家好,我有一个属性根,它是一个动态属性,可以与(vin1,vin2,vin3,vin4 .......)有所不同,后者甚至可以无穷无尽。

now when i do an export-csv i need to get output objects as below 现在,当我执行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

is there a way this can be accomplished?? 有没有办法可以做到这一点? like iterating within a for loop for all properties for Root??? 像在for循环中迭代Root的所有属性一样???

Try something like this: 尝试这样的事情:

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

This also shows the original array property (ex. "Root"). 这也显示了原始数组属性(例如“ Root”)。 If you want to exclude it, you need to use select -exclude ... inside the function. 如果要排除它,则需要在函数内部使用select -exclude ... However, this creates a new "pscustomobject" (you lose the original objecttype), so I didn't include this in the solution above. 但是,这将创建一个新的“ pscustomobject”(您将丢失原始的对象类型),因此我没有在上面的解决方案中包括它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM