arrays - 迭代PowerShell对象的子属性

标签 arrays powershell

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

嗨,大家好,我有一个属性根,它是一个动态属性,可以与(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的所有属性一样???

最佳答案

尝试这样的事情:

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”(您将丢失原始的对象类型),因此我没有在上面的解决方案中包括它。

关于arrays - 迭代PowerShell对象的子属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104495/

相关文章:

windows - 将 powershell 输出导出到文本文件

Java:从 JSON 解析数组的数组

python - 将二维数组中的每个值乘以另一个二维数组中的对应值

arrays - 带有没有空格的数组的 Powershell Move-Item

powershell - 如何在PowerShell中从数组创建ArrayList?

python - 为什么 pip 在 PyCharm 之外无法工作

python - 索引错误: list assignment index out of range - Python with an array

c++如何从 vector 中找到总和为给定数字的元素的最小数量

c - C 中带有指向结构体和结构体数组的指针的随机结果

powershell - 强制以管理员身份运行 PowerShell 脚本