PowerShell递归对象属性

PowerShell递归对象属性,powershell,object,recursion,Powershell,Object,Recursion,我需要一个递归地给出对象所有属性的方法。 不知道传输的对象有多少子对象 示例对象: $Car = [PSCustomObject] @{ Tire = [PSCustomObject] @{ Color = "Black" Count = 4 } SteeringWheel = [PSCustomObject]@{ Color = "Blue"

我需要一个递归地给出对象所有属性的方法。 不知道传输的对象有多少子对象

示例对象:

$Car = [PSCustomObject] @{
    Tire          = [PSCustomObject] @{
        Color = "Black"
        Count = 4
    }

    SteeringWheel = [PSCustomObject]@{
        Color   = "Blue"
        Buttons = 15
    }
}
非常感谢你

使用隐藏的psobject成员集枚举属性,然后递归:

函数解析属性 { param[ParameterValueFromPipeline][object]$InputObject 过程{ $InputObject.psobject.Properties中的foreach$prop{ [pscustomobject]@{ Name=$prop.Name Value=$prop.Value } 解析属性$prop.Value } } } 使用示例对象层次结构输出:

PS C:\>解析属性$Car 名称值 -- --- 轮胎@{颜色=黑色;计数=4} 黑色 长度5 第四点 SteeringWheel@{颜色=蓝色;按钮=15} 蓝色 长度4 按钮15 请注意,上面显示的函数并不努力防止无限循环递归引用,因此:

$a = [pscustomobject]@{b = [pscustomobject]@{a = $null}}
$a.b.a = $a
Resolve-Properties $a

将使您的CPU旋转

这是否回答了您的问题?